Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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?



Comment on Using eval, but always undef?
Select or Download Code
Re: Using eval, but always undef?
by choroba (Canon) 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.



        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?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2015-03-29 10:43 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (630 votes), past polls