(on perl v5.16.2)
(time ? @a=() : $a) = Foo();
I would expect the above to give an "Assignment to both a list and a scalar" error, but instead Foo is just evaluated in scalar context.
Leaving aside that this is poor code, can someone explain what's going on here?
Thanks!
Update: Thanks everyone for all the help. Again, with:
This is perl 5, version 16, subversion 2 (v5.16.2) built for x86_64-linux
I do:
~home>perl -MO=Concise, -ce '$z = (time ? @x=() : $x) = 365'
e <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) v:{ ->3
d <2> sassign vKS/2 ->e
b <2> sassign sKS/2 ->c
3 <$> const(IV 365) s ->4
- <1> null sKPRM*/1 ->b
5 <|> cond_expr(other->6) sKRM*/1 ->f
4 <0> time[t1] s ->5
a <2> aassign[t3] sKRMS* ->b
- <1> ex-list lK ->7
6 <0> pushmark s ->7
- <0> stub lP ->-
- <1> ex-list lK ->a
7 <0> pushmark s ->8
9 <1> rv2av[t2] lKRM*/1 ->a
8 <$> gv(*x) s ->9
- <1> ex-rv2sv sKRM*/1 ->-
f <$> gvsv(*x) s ->b
- <1> ex-rv2sv sKRM*/1 ->d
c <$> gvsv(*z) s ->d
-e syntax OK
versus
~home>perl -MO=Concise, -ce '$z = (time ? (@x=()) : $x) = 365'
Assignment to both a list and a scalar at -e line 1, at EOF
-e had compilation errors.
e <@> leave[1 ref] KP/REFC ->(end)
1 <0> enter ->2
2 <;> nextstate(main 1 -e:1) :{ ->3
d <2> sassign KS/2 ->e
b <2> sassign KS/2 ->c
3 <$> const(IV 365) s ->4
- <1> null KP/1 ->b
5 <|> cond_expr(other->6) K/1 ->f
4 <0> time[t1] s ->5
a <2> aassign[t3] KPS ->b
- <1> ex-list lK ->7
6 <0> pushmark s ->7
- <0> stub lP ->-
- <1> ex-list lK ->a
7 <0> pushmark s ->8
9 <1> rv2av[t2] lKRM*/1 ->a
8 <$> gv(*x) s ->9
- <1> ex-rv2sv sK/1 ->-
f <$> gvsv(*x) s ->b
- <1> ex-rv2sv sK/1 ->d
c <$> gvsv(*z) s ->d
There are several differences, starting with the fact that in the first case (the case where it compiles, but I would have expected it not to), apparently the assignment to the ternary is parsed as scalar context (s), where in the second case (the case where it gives compiler errors, as I would have expected), the context isn't indicated.
perlop indicates that for a conditional expression, the scalar/list context propagates down to the 2nd or 3rd argument. I suspect something wild is going on with parsing
"@x=()" and
"(@x=())" as scalar lvalues, where the first is legitimate, and the second isn't. This is really just a guess, though.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.