Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Using eval, but always undef?

by ultranerds (Friar)
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?



Replies are listed 'Best First'.
Re: Using eval, but always undef?
by LanX (Chancellor) 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.

Re: Using eval, but always undef?
by choroba (Chancellor) 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;

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 meditating upon the Monastery: (2)
As of 2016-09-26 00:51 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (474 votes). Check out past polls.