Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

To || or not to or and why.

by krisahoch (Deacon)
on Sep 15, 2003 at 21:53 UTC ( #291668=perlquestion: print w/ replies, xml ) Need Help??
krisahoch has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I have both a quick question and then an extended question.

First things first.

I want to pass a value to a class method. If the value is undef, then I want it to immediately return undef. This is generally something that I'd look up in the Camel book, but since it is not around, please help:(. I am torn between
#This my $RootDirectoryCanidate= shift() or return undef(); #and That my $RootDirectoryCanidate= shift() || return undef();
The question is which one should I use to accomplish my requirement?

Second things second

\\//\\//hy would one be better than the other. I have a vague memory relating to precedence, but (as you can tell since I am asking) I can remember it:<
Kristofer Hoch

Comment on To || or not to or and why.
Download Code
Re: To || or not to or and why.
by Abigail-II (Bishop) on Sep 15, 2003 at 22:12 UTC
    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

Re: To || or not to or and why.
by dws (Chancellor) on Sep 15, 2003 at 22:21 UTC
    I want to pass a value to a class method. If the value is undef, then I want it to immediately return undef.

    What's wrong with splitting this into two statements?

    my $RootDirectoryCandidate = shift(); return unless defined($RootDirectoryCandidate);
    You still get the immediate return, the result is more readable, and I really doubt you could measure a performance difference.

Re: To || or not to or and why.
by gjb (Vicar) on Sep 15, 2003 at 22:43 UTC

    Abigail-II and dws have valid points you should take to heart, but don't answer your original question which is about the difference between or and ||. The difference is in precedence, || has higher precedence than or. This can be found in the perlop manpage.

    Note: especially keep in mind the relative precende of or and || with respect to assignment.

    Just my 2 cents, -gjb-

      A good example of the difference in precedence of or and || is in the common perl idiom of
      open FILE, "file" or die;
      it is not the same as
      open FILE, "file" || die;
      since || has a higher precedence than open the "file" || die gets evaluated before the open. So it will not die even if the open fails. or has a lower precedence than open so the open gets evaluated first in the first example. To have the same effect using || you have to use
      open(FILE, "file") || die;

      --

      flounder

        Flounder,

        Thank you for that explaination. So if I am reading it right, then the would following be true?
         
        The following functions set the value of the return to 'default' by default. Then if was a parameter passed, $value gets set to that parameter. The differences between the two are:
        1. The juxteposition of the shift() statement and 'default'
        2. The use of the '||' and 'or' operators
        sub setValue_takeOne{ my $value = shift() || 'default'; return $value; } sub setValue_takeTwo{ my $value = 'default' or shift(); return $value; }

Re: To || or not to or and why.
by shenme (Priest) on Sep 15, 2003 at 22:56 UTC
    Looking through perlop I found an example showing one nastiness:
    @info = stat($file) || die; # oops, scalar sense of stat! @info = stat($file) or die; # better, now @info gets its due
    I guess an equivalent to your situation might be:
    my @Candidates = @_ or return; my @Candidates = @_ || return;
    The second will return undef or set the only element of @Candidates to the number of arguments passed in.   So in effect 'or' is allowing operators, including assignments, to be completely evaluated, without inducing any side-effects.

Re: To || or not to or and why.
by krisahoch (Deacon) on Sep 15, 2003 at 23:49 UTC

    Thank you all

    I think that I am going to go with dws's suggestion. I would have taken Abigail-II's but my method looks like this...
    sub Method{ my $MethodObject = shift(); #OO style my $RootDirectoryCandidate = shift(); return unless(defined ($RootDirectoryCanidate)); }
    Thank you all.
    Kristofer Hoch

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-07-14 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (255 votes), past polls