Front page | perl.perl5.porters |
Postings from November 2015
[perl #126749] FH: get more benefit from ||, &&
Thread Next
From:
KES
Date:
November 27, 2015 11:35
Subject:
[perl #126749] FH: get more benefit from ||, &&
Message ID:
rt-4.0.18-24248-1448624091-1138.126749-75-0@perl.org
# New Ticket Created by KES
# Please include the string: [perl #126749]
# in the subject line of all future correspondence about this issue.
# <URL: https://rt.perl.org/Ticket/Display.html?id=126749 >
To: perlbug@perl.org
Cc: kes-kes@yandex.ru
Subject: FH: get more benefit from ||, &&
From: kes-kes@yandex.ru
Reply-To: kes-kes@yandex.ru
Message-Id: <5.22.0_6261_1448609478@keswork>
This is a bug report for perl from kes-kes@yandex.ru,
generated with the help of perlbug 1.40 running under perl 5.22.0.
-----------------------------------------------------------------
[Please describe your issue here]
A few days ago I ran into the problem the expression does not work as
expected:
eval: $z=7; ( $fh ) = $z =~/^(\d+)$/ || $fh; print $fh;
result: 1
expected: 7
I have tried the DOC: http://perldoc.perl.org/perlop.html#C-style-Logical-Or
I have found only one interesting thing:
Scalar or list context propagates down to the right operand if it is evaluated.
I would never have thought, but thank to 'mst'. He point me to:
http://perldoc.perl.org/perlop.html#Logical-Defined-Or
And I have found I am interesting in:
@a = @b || @c; # this is wrong
@a = scalar(@b) || @c; # really meant this
@a = @b ? @b : @c; # this works fine, though
So behaviour for my case is documented and therefore should be expected and
my assumption is wrong. (sighs)
But what about DWIM (Do What I Mean) principle?
When I write '@a = @b || @c' I mean: assign first list, if it is empty assign other.
And I am surprised to have the list of (1) in @a
I ask people on the #perl channel and they argue that we need scalar to calcuate
the result of boolean expression so @b IS CALCULATED IN SCALAR CONTEXT
Diggig deeper and deeper...
When looking to assignment operators http://perldoc.perl.org/perlop.html#Assignment-Operators
Everybody will agree that:
$x += 2 is equivalent to $x = $x + 2
@x ||= (1,2,3) is equivalent to @x = @x || (1,2,3)
# @x += 2 This seems meaningless but maybe @x = map $_+2, @x # ??? =)
Use cases:
@x ||= (1,2,3); # Can't modify array dereference in logical or assignment (||=) at
print "@x\n";
@x = (0,1);
@x ||= (1,2,3); # Can't modify array dereference in logical or assignment (||=) at
print "@x\n";
@y = @y || (1,2,3);
print "@y\n"; # 1 2 3
@y = (0,1);
@y = @y || (1,2,3);
print "@y\n"; # 2
First surprise: why the short form does not work whereas long does?
Second suprise:
people on irc said
truth is a scalar concept. it can't work at all otherwise.
@y = (0,1);
print "@y" if @y = @y; # (0,1) <--- Why this works?? it is list, not scalar
I have not found info (http://perldoc.perl.org/perlsyn.html#Compound-Statements)
in which context the if EXPR is executed so I assume it requires the scalar
also I relay on the 'truth is a scalar concept'. Now I suppose this:
if scalar( EXPR )
Because of @y || @y also checks left @y for truth I am should be forced to suppose:
scalar( @y ) || @y
This is same as described here: http://perldoc.perl.org/perlop.html#Logical-Defined-Or
But stop, || is more than just check for truth, here we require the result
of expression. Actually two results required:
@y = @y || @y;
1. for ||
2. for =
Please pay attention and do not forget about 'if @y = @y' and about its two steps:
1. We calculate the expression in its own context
2. We force THE EXPRESSION RESULT into scalar to make truth concept to work
If we apply same logic @y = EXPR || EXPR:
1. We require the EXPR result in list context,
so calculate EXPR in list context
2. force THE EXPRESSION RESULT into scalar to make check by ||
if true return THE EXPRESSION RESULT else calculate EXPR in required context
in pseudo code that may look like (http://paste.scsys.co.uk/501911):
1 sub OR {
2 my( $left, $right ) = @_;
3
4 if( wantarray ) {
5 @result = $left->(); # list
6 return @result if @result;
7
8 return $right->();
9 }
10 else {
11 $result = $left->(); # scalar
12 return $result if $result;
13
14 return $right->();
15 }
16 }
17
18
19 @l = foo() OR bar(); # foo is called in list context
20 $s = foo() OR bar(); # foo is called in scalar context
The call context of fn_this should depend on context of ||
fn_this() || fn_that() # void
$x = fn_this() || fn_that() # scalar
@x = fn_this() || fn_that() # list
sub t {
print defined wantarray ?
wantarray ? 'LIST - ' : 'SCALAR - ':
'VOID - ';
undef;
}
Should be:
t || t; # VOID - VOID -
$x = t || t; # SCALAR - SCALAR -
@x = t || t; # LIST - LIST -
Instead of current:
t || t; # SCALAR - VOID -
$x = t || t; # SCALAR - SCALAR -
@x = t || t; # SCALAR - LIST -
What benefits we get:
@x ||= qw/ s o m e d e f a u l t s /; # ('s','o'...'s') if @x is empty
@x = $str =~ /^(\d)(\d)(\d)$/ || (); # (7,5,3) if $str eq '753', NOT (3)
@x = fn_this() || fn_that(); # faster: only one call to fn_this
@x = fn_this() ? fn_this() : fn_that() ; # slower: fn_this called twice
For the if statement there is not change in behaviour:
1 if @x || @y;
1 if t || t; # SCALAR - SCALAR -
PS. I have not thiat about but this logic may be applied to && also.
[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
category=core
severity=wishlist
---
Site configuration information for perl 5.22.0:
Configured by kes at Tue Sep 29 16:12:55 EEST 2015.
Summary of my perl5 (revision 5 version 22 subversion 0) configuration:
Platform:
osname=linux, osvers=3.13.0-37-generic, archname=x86_64-linux
uname='linux keswork 3.13.0-37-generic #64-ubuntu smp mon sep 22 21:28:38 utc 2014 x86_64 x86_64 x86_64 gnulinux '
config_args='-des -Dprefix=/home/kes/perl'
hint=recommended, useposix=true, d_sigaction=define
useithreads=undef, usemultiplicity=undef
use64bitint=define, use64bitall=define, uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O2',
cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
ccversion='', gccversion='4.8.4', gccosandvers=''
intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678, doublekind=3
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16, longdblkind=3
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='cc', ldflags =' -fstack-protector -L/usr/local/lib'
libpth=/usr/local/lib /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed /usr/include/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu /lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib
libs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
libc=libc-2.19.so, so=so, useshrplib=false, libperl=libperl.a
gnulibc_version='2.19'
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector'
---
@INC for perl 5.22.0:
/home/kes/perl5/lib/perl5/x86_64-linux
/home/kes/perl5/lib/perl5
/home/kes/perl/lib/site_perl/5.22.0/x86_64-linux
/home/kes/perl/lib/site_perl/5.22.0
/home/kes/perl/lib/5.22.0/x86_64-linux
/home/kes/perl/lib/5.22.0
.
---
Environment for perl 5.22.0:
HOME=/home/kes
LANG=ru_UA.UTF-8
LANGUAGE=en
LC_MESSAGES=en_US.UTF-8
LD_LIBRARY_PATH (unset)
LOGDIR (unset)
PATH=/home/kes/perl/bin:/home/kes/perl/bin:/home/kes/perl5/bin:/home/kes/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PERL5LIB=/home/kes/perl5/lib/perl5
PERLDB_OPTS=RemotePort=keswork:9001
PERL_BADLANG (unset)
PERL_MB_OPT=--install_base "/home/kes/perl5"
PERL_MM_OPT=INSTALL_BASE=/home/kes/perl5
SHELL=/bin/bash
Thread Next
-
[perl #126749] FH: get more benefit from ||, &&
by KES