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

Question about two operators

by Anonymous Monk
on Oct 16, 2007 at 14:15 UTC ( [id://645185]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have two questions about perl operators that I have never used. How do these operators work (links to other sites are fine)?

<<= What does this operator do?

Also,

What would the value of the statement below be and why would one use it?

10 || 34

Thank you

Replies are listed 'Best First'.
Re: Question about two operators
by Corion (Patriarch) on Oct 16, 2007 at 14:18 UTC

    See perlop, the documentation about Perl Operators.

    <<= is the assignment variant of the left-shift operator, just like += is the assignment variant of the addition operator.

    || returns the value of the leftmost expression that is true.

Re: Question about two operators
by mr_mischief (Monsignor) on Oct 16, 2007 at 14:45 UTC
    Aside from what Corion said, I'll point out that one wouldn't use the expression: 10 || 34 as it is written there. That will always evaluate to 10. However, you'll often see in code things like any of the following when you're not sure the value on the left will have a nonzero value.
    $var || 1; $foo || $bar; $rock ||= 42; $paper ||= $scissors;
      $rock ||= 42;
      how auspicious, because while 10 || 34 is 10, 10 | 34 is 42!

Log In?
Username:
Password:

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

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

    No recent polls found