in reply to my within brackets
> But, is there some subtle difference my quick test has not uncovered?
It's the same and it's explicitly documented to be so.
See my VARLIST
The rest is standard list assignment.
Re^2: my within brackets
by AnomalousMonk (Archbishop) on Oct 14, 2021 at 23:06 UTC
|
Win8 Strawberry 5.8.9.5 (32) Thu 10/14/2021 17:49:15
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings -MO=Deparse,-p
my ($u, $v) = split /,/, 'U,V';
print "'$u' '$v' \n";
(my $w, my $x) = split /,/, 'W,X';
print "'$w' '$x' \n";
^Z
use warnings;
use strict 'refs';
(my($u, $v) = split(/,/, 'U,V', 3));
print("'${u}' '${v}' \n");
(my($w, $x) = split(/,/, 'W,X', 3));
print("'${w}' '${x}' \n");
- syntax OK
Win8 Strawberry 5.8.9.5 (32) Thu 10/14/2021 18:54:22
C:\@Work\Perl\monks
>perl -Mstrict -Mwarnings
my ($u, $v) = split /,/, 'U,V';
print "'$u' '$v' \n";
(my $w, my $x) = split /,/, 'W,X';
print "'$w' '$x' \n";
^Z
'U' 'V'
'W' 'X'
Same results with Perl version 5.30.3.1.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
D:\tmp\pm>perl -Mstrict -Mwarnings -MO=Concise
my ($u, $v) = split /,/, 'U,V';
__END__
9 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter v ->2
2 <;> nextstate(main 1 -:1) v:*,&,{,x*,x&,x$,$ ->3
8 <2> aassign[t4] vKS ->9
- <1> ex-list lK ->7
3 <0> pushmark s ->4
6 </> split(/","/)[t3] lK/IMPLIM ->7
4 <$> const[PV "U,V"] s ->5
5 <$> const[IV 3] s ->6
- <1> ex-list lKPRM* ->8
7 <0> padrange[$u:1,2; $v:1,2] RM/LVINTRO,range=2 ->8
- <0> padsv[$u:1,2] sRM*/LVINTRO ->-
- <0> padsv[$v:1,2] sRM*/LVINTRO ->-
- syntax OK
D:\tmp\pm>perl -Mstrict -Mwarnings -MO=Concise
(my $u, my $v) = split /,/, 'U,V';
__END__
9 <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter v ->2
2 <;> nextstate(main 1 -:1) v:*,&,{,x*,x&,x$,$ ->3
8 <2> aassign[t4] vKS ->9
- <1> ex-list lK ->7
3 <0> pushmark s ->4
6 </> split(/","/)[t3] lK/IMPLIM ->7
4 <$> const[PV "U,V"] s ->5
5 <$> const[IV 3] s ->6
- <1> ex-list lKPRM* ->8
7 <0> padrange[$u:1,2; $v:1,2] RM/LVINTRO,range=2 ->8
- <0> padsv[$u:1,2] sRM*/LVINTRO ->-
- <0> padsv[$v:1,2] sRM*/LVINTRO ->-
- syntax OK
D:\tmp\pm>
Now, spot the difference! ;-)
| [reply] [d/l] |
|
$ diff -u \
<( perl -Mstrict -Mwarnings -MO=Concise -e'my ($u, $v) = split /
+,/, "U,V";' 2>&1 ) \
<( perl -Mstrict -Mwarnings -MO=Concise -e'(my $u, my $v) = split /
+,/, "U,V";' 2>&1 ) \
&& echo same
same
PS, I usually use -MO=Concise,-exec
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:*,&,{,x*,x&,x$,$
3 <0> pushmark s
4 <$> const[PV "U,V"] s
5 <$> const[IV 3] s
6 </> split(/","/)[t3] lK/IMPLIM
7 <0> padrange[$u:1,2; $v:1,2] RM/LVINTRO,range=2
8 <2> aassign[t4] vKS
9 <@> leave[1 ref] vKP/REFC
-e syntax OK
| [reply] [d/l] [select] |
|
|
|
Re^2: my within brackets
by Bod (Parson) on Oct 14, 2021 at 19:33 UTC
|
it's explicitly documented
I had looked at the docs for my and didn't find it very explicit at all. It says "If more than one variable is listed, the list must be placed in parentheses" but makes no reference to having the my statement inside the parenthesis. Or am I missing something subtle in the documentation?
This declaration seems unusual and I don't recall seeing it anywhere else which is why it stood out.
| [reply] [d/l] [select] |
|
this
> "If more than one variable is listed, the list must be placed in parentheses"
means:
If VARLIST is
$x,$y,$z
you must write
my ($x,$y,$z)
The alternative
my $x,$y,$z
would only declare $x because of precedence, like
(my $x),$y,$z
Hence an alternative to what we want is
my $x,my $y,my $z
But since we need a list assignment on the LHS, we still need to put it all into parens
(my $x,my $y,my $z) = split ...
because this
my $x,my $y,my $z = split ...
would only assign to $z because of precedence°, like
(my $x),(my $y),(my $z = split ...)
HTH! :)
Errata
s/declare/assign to/ choroba++
UPDATES
°) Even worse, it would only be a scalar assignment to $z, i.e. the number of possible splits.
| [reply] [d/l] [select] |
|
|