Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: To || or not to or and why.

by Abigail-II (Bishop)
on Sep 15, 2003 at 22:12 UTC ( #291670=note: print w/replies, xml ) Need Help??


in reply to To || or not to or and why.

my $RootDirectoryCanidate= shift() or return undef(); my $RootDirectoryCanidate= shift() || return undef();
The question is which one should I use to accomplish my requirement?

Neither. The both return undef if no argument is passed, or if the first argument is 0, "0", or the empty string. What I would do is:

sub method { return if @_ && !defined $_ [0]; my $root_directory_candidate = shift; ... }
Now, if you want to return right away if nothing is passed as well, make the first line in the method:
return unless defined $_ [0];

Note that I didn't do "return undef". If you do that, and you call the sub in list context, it will return a *true* value (a one element list). But a return without arguments will return an empty list in list context, and undef in scalar context.

Abigail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-07-04 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?