Re: Meaning of //
by huck (Prior) on Oct 17, 2019 at 17:10 UTC
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] [d/l] |
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. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
... 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: <%-{-{-{-<
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Meaning of //
by ikegami (Patriarch) on Oct 19, 2019 at 13:40 UTC
|
return 1 unless ($flag // 0);
... if $HAS_FOCUS // '';
are better written as
return 1 unless $flag;
... if $HAS_FOCUS;
| [reply] [Watch: Dir/Any] [d/l] [select] |
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." | [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] |