Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: I can't see why this shorthand doesn't behave like the verbose form

by isync (Hermit)
on Mar 25, 2012 at 11:53 UTC ( [id://961481]=note: print w/replies, xml ) Need Help??


in reply to I can't see why this shorthand doesn't behave like the verbose form

@Ken: I think you are right - it's time to look at JSON and SQL.

Today I've found the time to hack together a simple test script that doesn't exhibit the original problem. Here, when the variable is properly declared (or not) and populated in perl context, shorthand and longform behave the same.

Also, I've removed the noise of the eval and the defined() check, the latter I threw in anyway down the road when the simple check didn't seem to suffice.

In my production code the related_files data comes from a SQL field that is parsed based on the test if($sql->{field}), so I think that there's a slight difference how perl regards this field as defined, existing or true. But I am not so inclined to find out why exactly the shorthand doesn't work. As the more verbose code just works. Also, might also be an unrelated error I am overseeing. I'll post if there's a find in the future.
#! perl use Data::Dumper; use JSON::XS; my $asset = { ## rename this var to something else to test the 'undefined' c +ase related_files => '{ "firstName": "John", "lastName" : "Smith", "age" : 25, "address" : { "streetAddress": "21 2nd Street", "city" : "New York", "state" : "NY", "postalCode" : "10021" }, "phoneNumber": [ { "type" : "home", "number": "212 555-1234" }, { "type" : "fax", "number": "646 555-4567" } ] }' }; my $ref; if($asset->{related_files}){ $ref = decode_json($asset->{related_files}); }else{ $ref = []; } print Dumper($ref); ## simpler shorthand-code doesn't work, why? my $ref = $asset->{related_files} ? decode_json($asset->{related_files +}) : []; print Dumper($ref);

Replies are listed 'Best First'.
Re^2: I can't see why this shorthand doesn't behave like the verbose form
by tobyink (Canon) on Mar 25, 2012 at 14:52 UTC

    Really? Your script works as expected on this machine. I suppose you might have a severely broken JSON::XS - that's the only place I can see it possibly breaking. Tried JSON::PP?

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: I can't see why this shorthand doesn't behave like the verbose form
by Anonymous Monk on Mar 25, 2012 at 14:26 UTC

    Here is a demonstration of it working, and some alternatives :)

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; use JSON::XS; my $ref = { foo => q{[1,2,3]} }; for my $key( qw/ bar foo /) { { my $foo; if( $ref->{$key} ){ $foo = decode_json( $ref->{$key} ); } else { $foo = []; } dd $foo; } { my $foo = []; if( $ref->{$key} ){ $foo = decode_json( $ref->{$key} ); } dd $foo; } { my $foo = $ref->{$key} ? decode_json( $ref->{$key} ) : []; dd $foo; } { my $foo = eval { decode_json( $ref->{$key} ) } || []; dd $foo; } } __END__ [] [] [] [] [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://961481]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found