Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Rosetta Code Error or $n // 100

by GrandFather (Sage)
on Mar 24, 2019 at 20:16 UTC ( #1231624=note: print w/replies, xml ) Need Help??


in reply to Rosetta Code Error or $n // 100

They are really the same question. Passing only two arguments gets you an undef for the third argument. // is the defined or which returns the left hand argument if it is defined ($_[2] in this case) or the right hand argument if the left is undefined (100 in this case). So the expression returns 100 if the third argument is missing (undef).

It is commonly used, as in the example code, to provide a default value when a variable or return from a sub may be undefined.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: Rosetta Code Error or $n // 100
by pgmer6809 (Novice) on Mar 24, 2019 at 21:11 UTC

    Thanks. Either the code as posted on Rosetta has a typo // instead of || or I misread it. Once I realized that the // should be read as || it all made sense. Thanks again. Those replies were quicker than my thinking process. :(!

      No. || and // are two distinct operators. The first one checks whether its first argument is true, the second one whether it's defined; if not, they return the second argument. The difference becomes apparent with defined false values like 0:
      choroba@still ~ $ perl -wE 'say 0 || 1' 1 choroba@still ~ $ perl -wE 'say 0 // 1' 0

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2019-11-23 02:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (113 votes). Check out past polls.

    Notices?