I need to pass array of hash references to php so that php can used that data.
What I doing?
I am serializing perl array of hash references using PHP::Serialize module and storing into database. But while reading it i am facing issue.
#!/usr/local/bin/perl
use PHP::Serialization qw(serialize unserialize);
my @rss_feed_result = ({
'data'=> 'here',
'id'=> 1,
},
);
my $rss_feed_result = serialize(\@rss_feed_result);
//storing it to database then
Now I am reading the same data in php by unserialize it and getting below output after unserialize which is not proper.
<?php
.....read data from database into $perl_serialize_data
$perl_serialize_data = "HERE PERL SERIALIZE DATA";
print_r(unserialize($perl_serialize_data));
?>
Below is the output.
(
[0] => {
[1] => 'data'=>
[2] => 'here',
[3] => 'id'=>
[4] => 1,
[5] => },
)
How to fix this?
Your RSS feed parser would return a hash, but you use
my @rss_feed_result = qw(
{
'data'=> 'here',
'id'=> 1,
},
);
which is the same as
my @rss_feed_result = (split ' ', q(
{
'data'=> 'here',
'id'=> 1,
},
));
It would probably work fine if you actually had a reference to a hash.
my @rss_feed_result = (
{
'data'=> 'here',
'id'=> 1,
}
);
(Parens optional.)
If you were using use warnings
you would have gotten this warning:
Possible attempt to separate words with commas at foo.pl line 1.
With qw()
you are trying to quote that data structure as single words. The result looks like this in Perl:
("{", "'data'=>", "'here',", "'id'=>", "1,", "},")
But you don't want a list. You want the data structure. Instead, you will probably want to do this:
use strict;
use warnings;
use PHP::Serialization qw(serialize unserialize);
my $rss_feed_result = {
'data' => 'here',
'id' => 1,
};
my $php = serialize($rss_feed_result);
If you have a list of these hash refs, you can still use your array.
my @rss_feed_results = (
{
'data' => 'here',
'id' => 1,
},
{
'data' => 'there',
'id' => 2,
},
{
'data' => 'over yonder',
'id' => 3,
},
);
my $php = serialize(\@rss_feed_results);