Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

What's the meaning of die ref $res... ?

by miguelele (Beadle)
on Jan 12, 2012 at 16:54 UTC ( #947581=perlquestion: print w/ replies, xml ) Need Help??
miguelele has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am handling errors in SOAP::LITE with the help of http://guide.soaplite.com/#error%20handling It works as I need, but I am stuck trying to understand this block of Perl code:
on_fault => sub { my($soap, $res) = @_; eval { die ref $res ? $res->faultstring : $soap->transport->status + }; return ref $res ? $res : new SOAP::SOM; };
Specially, after it dies, I do not understand the meaning of ref $res ? $res->faultstring : $soap->transport->status Is it referencing or something?

Any didactic help will be really appreciated.

Regards

Comment on What's the meaning of die ref $res... ?
Select or Download Code
Re: What's the meaning of die ref $res... ?
by Utilitarian (Vicar) on Jan 12, 2012 at 17:01 UTC
    Look for the Ternary operator in the perlop doc.
    ref $res ? $res->faultstring : $soap->transport->status
    Could be written as
    if (ref $res){ #$res is a reference and not a scalar die $res->faultstring; # exit printing the fault } else{ die $soap->transport->status; #Exit with the current transport statu +s of the SOAP object }
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      Thank you, I get it now! It is no easy to make a search for "ref" or "?" or ":"
      Regards
        Indeed, until you know what it's called (at which point you probably understand it) searching for the ternary op can be difficult ;)
        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: What's the meaning of die ref $res... ?
by BrowserUk (Pope) on Jan 12, 2012 at 17:06 UTC

    If you feed the code you are having trouble with in to Deparse,-p you get:

    C:\>perl -MO=Deparse,-p -e"die ref $res ? $res->faultstring : $soap->t +ransport->status" die((ref($res) ? $res->faultstring : $soap->transport->status)); -e syntax OK

    And if we add a little horizontal whitespace to that:

    die( ( ref( $res ) ? $res->faultstring : $soap->transport->status ) );

    You can see that the ref( $res ) part is a conditional test to see if $res is a reference.

    If it is, then it passes $res->faultstring to die.

    Otherwise it passes: $soap->transport->status to die.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-09-16 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (36 votes), past polls