Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Meaning of //

by hanspr (Sexton)
on Oct 17, 2019 at 16:55 UTC ( [id://11107612]=perlquestion: print w/replies, xml ) Need Help??

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

Im revising a code written in perl and I'm finding the use of "//" in different ways: tests, shift, others.

return 1 unless ($flag // 0) return 1 unless ($flag // 1) if $HAS_FOCUS // '' $socket = shift // $SOCKET_CLIENT;

I have searched and I do not find what does "//" means. In what section of the documentations is this explained

Replies are listed 'Best First'.
Re: Meaning of //
by huck (Prior) on Oct 17, 2019 at 17:10 UTC
      Thanks a lot
Re: Meaning of //
by Discipulus (Canon) on Oct 17, 2019 at 18:19 UTC
    Hola hanspr,

    this useful feature was added in 5.10 and it is used also very conveniently directly in the assignements:

    $val //= 42;

    see also defined or: // and //=

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Meaning of //
by stevieb (Canon) on Oct 17, 2019 at 20:31 UTC

    It's the only reason I switched my more complex software to 5.10 from 5.8 minimum requirement. It alleviates doing stuff like:

    sub func { my ($thing) = @_; $thing = defined $thing ? $thing : 'non-thing'; # or if (defined $thing){ $thing = $thing; } else { $thing = 'non-thing'; } }

    Much cleaner to use the previously-mentioned defined-or:

    sub func { my ($thing) = @_; $thing //= 'non-thing'; }

    That might not look like much of a difference, but add some complexity and other rules surrounding incoming variables and you can significantly reduce the keystrokes, code complexity and most important, readability, especially if in loop-type scenarios.

      ... cleaner ...

      Cleaner when used to do something reasonable as in your examples, but I hope hanspr never actually saw some of the examples given in the OP. As pointed out by pryrt,
          return 1 unless ($flag // 0);
      is exactly equivalent to
          return 1 unless $flag;
      (except more confusing), and  ... if $HAS_FOCUS // ''; likewise.


      Give a man a fish:  <%-{-{-{-<

Re: Meaning of //
by ikegami (Patriarch) on Oct 19, 2019 at 13:40 UTC

    undef is false, so

    return 1 unless ($flag // 0); ... if $HAS_FOCUS // '';
    are better written as
    return 1 unless $flag; ... if $HAS_FOCUS;
Re: Meaning of //
by rsFalse (Chaplain) on Oct 20, 2019 at 11:33 UTC
    I use '//' sometimes to avoid warnings, e.g.:
    perl -wle 'my $number; print 1 if 0 + ( $number // 0 )'
    ..e.g. in bigger expressions.
    Otherwise I get: "Use of uninitialized value $number in addition (+) at -e line 1."
Re: Meaning of //
by LanX (Saint) on Oct 17, 2019 at 19:43 UTC
    Other told you what it does, though the author attempted to do strange things.

    Something or 1 is always true, so this

    return 1 unless ($flag // 1)

    will never happen.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Except when $flag is false.
      C:\usr\local\share\PassThru\perl>perl -le "print +1//55"
      1
      
      C:\usr\local\share\PassThru\perl>perl -le "print +undef//55"
      55
      
      C:\usr\local\share\PassThru\perl>perl -le "print +0//55"
      0
      
        > Except when $flag is false.

        You mean defined but false, but you are right, I was wrong.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Meaning of //
by hanspr (Sexton) on Nov 28, 2019 at 15:53 UTC

    Thanks for all your comments and examples, I did understand its use.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-03-28 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found