Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Value of a statement with modifier

by davidrw (Prior)
on Jan 22, 2007 at 16:56 UTC ( [id://595941]=note: print w/replies, xml ) Need Help??


in reply to Value of a statement with modifier

from perlsub: "The return value of a subroutine is the value of the last expression evaluated."

this is a nice and subtle example (i had to run it too).. $_[0] is the last expression evaluated when the unless fails, so the sub just ends up returning the first argument..

good example for always using explict returns ...
return $_[0] ? undef : 'hello'; # or: return 'hello' unless $_[0]; return;

Replies are listed 'Best First'.
Re^2: Value of a statement with modifier
by LTjake (Prior) on Jan 22, 2007 at 18:18 UTC

    A small quibble, but I believe the first example should be written as:

    return $_[ 0 ] ? () : 'hello';

    --
    "Go up to the next female stranger you see and tell her that her "body is a wonderland."
    My hypothesis is that she’ll be too busy laughing at you to even bother slapping you.
    " (src)

        Returning undef for false is more broken than returning empty lists for false. At least an empty list is easily promoted to a false or undef value but an undef value in a list is not as easily demoted to a false value. I think all the people returning undef for false are shortsighted and I hope they never get their hands on my code.

        sub empty {} if ( empty() ) { ... } # ok @foo = empty(); if ( @foo ) { ... } # ok sub returns_undef { undef } if ( returns_undef() ) { ... } # ok @foo = returns_undef(); if ( @foo ) { ... } # NOT OK if ( grep { defined } @foo ) { ... } # oh ok, now it's "better".

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

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

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

    No recent polls found