Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

shift and logical or

by jonagondos (Novice)
on Nov 16, 2012 at 22:53 UTC ( #1004257=perlquestion: print w/replies, xml ) Need Help??
jonagondos has asked for the wisdom of the Perl Monks concerning the following question:

i understand the shift and logical or || but i keep seeing this type of stuff my $host = shift || 'localhost'; my $port = shift || 200; what is the combination of shift and || doing?

Replies are listed 'Best First'.
Re: shift and logical or
by 2teez (Vicar) on Nov 16, 2012 at 23:14 UTC

    Since you understand the shift and logical or ||, then maybe a simple example will illustrate the usage:

    use warnings; use strict; my $age = 15; print get_age($age); # my age is: 15 print "\n"; print get_age(); # my age is: 200 sub get_age { my $my_age = shift || 200; return " my age is: ", $my_age; }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: shift and logical or
by Kenosis (Priest) on Nov 16, 2012 at 23:19 UTC

    Consider the following:

    use strict; use warnings; printSomething('Hello, world.'); printSomething(); sub printSomething { my $string = shift || 'Nothing was sent.'; print $string, "\n"; }

    First output:

    Hello, world.

    Second output:

    Nothing was sent.

    In subroutines, shift takes the first element off @_. In the case above, $string is initialized with whatever is sent to the subroutine. However, if nothing is sent, $string is initialized with 'Nothing was sent.' This is because of the logical or operation in Perl. If the first disjunct (shift) evaluates to true, then Perl stops there, since the overall express is true. However if the first disjunct evaluates to false (nothing to shift), Perl moves to the second disjunct. Thus, printSomething() prints Nothing was sent. Here's another way to 'say' this: the variable $string equals either what was sent to the subroutine or the string 'Nothing was sent.'

Re: shift and logical or
by bulk88 (Priest) on Nov 16, 2012 at 23:08 UTC
    If shift returns undef or false, then evaluate right side, assign result of right side to $host. Default parameters basically.
Re: shift and logical or
by rjt (Deacon) on Nov 17, 2012 at 13:46 UTC

    Good replies so far, so I won't steal anyone's monastic thunder, although I'll add that Perls >= 5.10 have the logical defined-or operator, //. Often this is what you really want, but since there is a lot of code in the wild that pre-dates it, you may notice disproportionate use of ||.

    The difference is simple:

    1. Use $a //= 'foo'; when you want to assign 'foo' to $a if $a is not defined.
    2. Use $a ||= 'foo'; when you want to assign 'foo' to $a if $a has a false value, such as zero or an empty string.
Re: shift and logical or
by mhearse (Chaplain) on Nov 17, 2012 at 01:20 UTC

    my $val ||= "undefined"

    Which is equivalent to:

    if (!$val) { $val = "undefined"; }

    Blocks of code like that tend to clutter up your code.

    Also consider:

    $val = ($val) ? "defined" : "undefined";

    Which is equivalent to:

    if ($val) { $val = "defined"; } else { $val = "undefined"; }

    Also, remember perl evaluates expressions for truth.

      Defined, undefined or false?

      use strict; use warnings; my $val = 0; print +($val) ? "defined" : "undefined"; # prints undefined print "\n"; print +(defined $val) ? "defined" : "undefined"; # prints defined undef $val; print "\n"; print +(defined $val) ? "defined" : "undefined"; # prints undefined
Re: shift and logical or
by GrandFather (Sage) on Nov 20, 2012 at 00:46 UTC

    Do you understand Perl's logical ||? The key point in the context you are having trouble understanding is that the tested value is returned. Other languages with a logical or return a boolean value. Perl returns the actual value. So:

    0 || 'wibble'

    returns the string wibble, not 1, -1 or true as you might get in other languages. That makes logical or very useful for providing default values.

    True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004257]
Approved by bulk88
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-05-20 23:51 GMT
Find Nodes?
    Voting Booth?