Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

String to PERL data structure

by perl_lover (Chaplain)
on Jul 13, 2007 at 05:25 UTC ( #626373=perlquestion: print w/ replies, xml ) Need Help??
perl_lover has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
In a legacy application PHP is communicating to PERL through Pipes. PHP file outputs a string data and in PERL we need to create the data structure. I tried with Data::Dumper but it is always returning undef. here is the string which PHP file gives
Array ( [0] => Array ( [attrs] => Array ( [0] => Array ( [key] => rat1 [value] => -10 [options] => 0 [type] => 0 ) [1] => Array ( [key] => rat1 [value] => -10 [options] => 0 [type] => 0 ) ) [mesg_id] => 1 [parent_id] => 0 [thread_id] => 1 [cat_id] => 3 [folder_id] => 0 [status] => 4 ) )
Is there any way to Dump this structure into PERL data structure ?

Comment on String to PERL data structure
Download Code
Re: String to PERL data structure
by Zaxo (Archbishop) on Jul 13, 2007 at 06:10 UTC

    The problem is that this PHP is not written in Perl. You will need some kind of parser for the subset of PHP which appears in your dumps. I did not find an Inline::PHP on cpan, but that is the kind of thing you will need. Parse::RecDescent may help if you need to write one.

    After Compline,
    Zaxo

Re: String to Perl data structure
by chromatic (Archbishop) on Jul 13, 2007 at 06:12 UTC

    Can your PHP application emit JSON or YAML? If so, that's the easiest approach. Otherwise, you need to write some sort of parser for whatever data structure that string represents in Perl. Data::Dumper produces Perl code that, when evaled, produces a Perl data structure. Your example is not Perl code.

Re: String to PERL data structure
by fmerges (Chaplain) on Jul 13, 2007 at 08:41 UTC

    Hi,

    As chromatic said, I would also go for YAML or JSON.

    Regards,

    fmerges at irc.freenode.net
Re: String to PERL data structure
by shmem (Canon) on Jul 13, 2007 at 08:46 UTC
    Straight forward, quick and dirty. Usual caveats for string eval apply.
    use Data::Dump::Streamer; my $string; while (<DATA>) { chomp; s/\r//; # append a comma to lines not ending in 'Array' or '(' # or being (other than containing whitespace) empty lines $_ .= ',' unless /(Array|\(|^\s*)$/; $string .= $_."\n"; } sub Array { my %h; while(@_) { my ($key) = @{shift(@_)}; $h {$key} = shift(@_); } \%h; } print $string,"\n"; my $ref = eval $string; Dump $ref; __DATA__ Array ( [0] => Array ( [attrs] => Array ( [0] => Array ( [key] => rat1 [value] => -10 [options] => 0 [type] => 0 ) [1] => Array ( [key] => rat1 [value] => -10 [options] => 0 [type] => 0 ) ) [mesg_id] => 1 [parent_id] => 0 [thread_id] => 1 [cat_id] => 3 [folder_id] => 0 [status] => 4 ) )

    Output:

    Array ( [0] => Array ( [attrs] => Array ( [0] => Array ( [key] => rat1, [value] => -10, [options] => 0, [type] => 0, ), [1] => Array ( [key] => rat1, [value] => -10, [options] => 0, [type] => 0, ), ), [mesg_id] => 1, [parent_id] => 0, [thread_id] => 1, [cat_id] => 3, [folder_id] => 0, [status] => 4, ), ), $HASH1 = { 0 => { attrs => { 0 => { key => 'rat1', options => 0, type => 0, value => -10 }, 1 => { key => 'rat1', options => 0, type => 0, value => -10 } }, cat_id => 3, folder_id => 0, mesg_id => 1, parent_id => 0, status => 4, thread_id => 1 } };

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://626373]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-21 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (167 votes), past polls