Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Using eval, but always undef?

by ultranerds (Pilgrim)
on Mar 01, 2012 at 17:39 UTC ( #957276=perlquestion: print w/ replies, xml ) Need Help??
ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys/gals,

I'm trying to work out why my "eval" wont work. Basically I am grabbing an XML file, but for some reason they return errors ( no results) like so:

{ "error":{ "type":"ServiceException", "code":108, "message":"(#108) No results founds" } }
... but the actual results as XML (which XMLIn gives a fatal error on, as obviously thats not XML!)

Anyway, my reason for posting is that I can't see to get that above string to convert into a hash so I can check if there is an error:

my $test = eval q|{ "error":{ "type":"ServiceException", "code":108, "message":"(#108) No results founds" } }|; use Data::Dumper; print Dumper($test);
Always returns:

$VAR1 = undef;

Anyone got any ideas?

TIA

Andy

Comment on Using eval, but always undef?
Select or Download Code
Re: Using eval, but always undef?
by choroba (Abbot) on Mar 01, 2012 at 17:45 UTC
    perl -e 'my $test; eval q|$test = { "error":{ "type":"ServiceException", "code":108, "message":"(#108) No results founds" } }|; print "$@\n"; use Data::Dumper; print Dumper($test);'
    syntax error at (eval 1) line 2, near ""error":" $VAR1 = undef;
Re: Using eval, but always undef?
by LanX (Canon) on Mar 01, 2012 at 17:46 UTC
    in perl it's => not : separating keys and values.

    maybe you wanna use a JSON module for conversion?

    Cheers Rolf

    ) normally, commas are allowed if you quote the key

      Mmm ok, I was hoping it was simpler than that. Maybe I'll just do:

      $test =~ /\Q"type":"ServiceException",/

      ...on the XML string, so I can see if it is as "bad" file :)

      Thanks for the replies though.

      Cheers

      Andy

        It is simple (JSON).

        use JSON; my $json_string = q|{ "error":{ "type":"ServiceException", "code":108, "message":"(#108) No results founds" } }|; my $perl_ref = decode_json($json_string); print $perl_ref->{error}{type}, $/; __END__ ServiceException

        Don't fudge it. Use the right tools. Everything else is false economy.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-07-31 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls