Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
[robby_dobby]: I was falling asleep at my chair and threw out that one bit about participating in a YAPC. Things went from there :-)
[erix]: never too late to pull legs
[LanX]: Official YAPC Africa 2013 Announcement
erix always game to pull legs from sleeping cells
[erix]: hm nice might do a spot of diving - Red Sea is said to be beautiful
[LanX]: well too many terrorist tourists
[erix]: I guess I can pass for a native
[LanX]: talking about destruction of diving spots
[LanX]: Nodes to consider
[erix]: oops - got to run, see you later

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2017-04-24 16:17 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (442 votes). Check out past polls.