for @{ my $x } on Perl 5.10 (bug?)

J

John Bokma

perl -e 'use strict; use warnings; print for @{ my $x }'
Can't use an undefined value as an ARRAY reference at -e line 1.

This is perl, v5.8.8 built for x86_64-linux-thread-multi


perl -e 'use strict; use warnings; print for @{ my $x }'

This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi

Is this a known bug? At least, I assume that the latter working is a bug.
 
C

C.DeRykus

perl -e 'use strict; use warnings; print for @{ my $x }'
Can't use an undefined value as an ARRAY reference at -e line 1.

This is perl, v5.8.8 built for x86_64-linux-thread-multi

perl -e 'use strict; use warnings; print for @{ my $x }'

This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi

Is this a known bug? At least, I assume that the latter working is a bug.

Same with 5.12.2:

perl -Mstrict -wle "print if @{my $x}"
Can't use an undefined value as an ARRAY reference at -e line 1.

perl -Mstrict -wle "print for @{my $x}"


At the very least it seems quirky that the
former fails and the latter doesn't.
 
J

John Bokma

C.DeRykus said:
Same with 5.12.2:

perl -Mstrict -wle "print if @{my $x}"
Can't use an undefined value as an ARRAY reference at -e line 1.

perl -Mstrict -wle "print for @{my $x}"

At the very least it seems quirky that the
former fails and the latter doesn't.

Thanks Charles, I've reported it using perlbug.

perl -e'use strict; use warnings; print while @{ my $x };'
Can't use an undefined value as an ARRAY reference at -e line 1.

perl -e'use strict; use warnings; for ( @{ my $x } ) { print }'
 
J

John Bokma

John Bokma said:
Thanks Charles, I've reported it using perlbug.

http://rt.perl.org/rt3/Ticket/Display.html?id=89024

It looks like it's a feature. Haven't checked the change logs yet, but I
have the feeling that this was done to make

my $x->{ foo } = 3;

work (which I am OK with), and as a side effect (?) makes also:

perl -e 'use strict; use warnings; print for @{ my $x }'
perl -e 'use strict; use warnings; my $x; sub foo { my @bar = @_; };
foo( @{ my $x } );'

work (and probably some other things I forgot).

I was leaning towards a bug (when I knew about the for issue), but no
longer so sure. It does sound logical, the new way, but it makes also
IMO Perl a little more magic.
 
T

Ted Zlatanov

JB> It looks like it's a feature. Haven't checked the change logs yet, but I
JB> have the feeling that this was done to make

JB> my $x->{ foo } = 3;

JB> work (which I am OK with)

Ugh. That's some nasty syntactic syrup.

Ted
 
J

John Bokma

Ted Zlatanov said:
JB> It looks like it's a feature. Haven't checked the change logs yet, but I
JB> have the feeling that this was done to make

JB> my $x->{ foo } = 3;

JB> work (which I am OK with)

Ugh. That's some nasty syntactic syrup.

Depends:

my $x = { foo => bar( ... ) };

versus

my $x->{ foo } = bar( ... );

I think the latter is a bit more pleasant to the (well, my) eyes.
 
C

C.DeRykus

http://rt.perl.org/rt3/Ticket/Display.html?id=89024

It looks like it's a feature. Haven't checked the change logs yet, but I
have the feeling that this was done to make

my $x->{ foo } = 3;

But the case above is an lvalue so $x autovivifies.

In the other example though:

perl -Mstrict -wle "print if @{my $x}"

'strict' generates a "Can't use an undefined
value as an ARRAY reference" and autoviv will
occur only without 'strict'.

Make the above an lvalue and there's no error:

perl -Mstrict -wle "print if @{my $x}=()"


Similarly, 'use strict' should also complain
in the case below since there's no lvalue:

perl -Mstrict -wle "print for @{my $x}"


Yet it doesn't which seems quirky to me.

work (which I am OK with), and as a side effect (?) makes also:

perl -e 'use strict; use warnings; print for @{ my $x }'
perl -e 'use strict; use warnings; my $x; sub foo { my @bar = @_; };
         foo( @{ my $x } );'

work (and probably some other things I forgot).

I was leaning towards a bug (when I knew about the for issue), but no
longer so sure. It does sound logical, the new way, but it makes also
IMO Perl a little more magic.

Magic's needed at times, but this particular
magic seems to trump consistency. It would
seem preferable to me to just loosen 'strict'
to ignore the error that's now thrown for:

perl -Mstrict -wle "print if @{my $x}"
 
J

John Bokma

C.DeRykus said:
But the case above is an lvalue so $x autovivifies.

In the other example though:

perl -Mstrict -wle "print if @{my $x}"

'strict' generates a "Can't use an undefined
value as an ARRAY reference" and autoviv will
occur only without 'strict'.

Make the above an lvalue and there's no error:

perl -Mstrict -wle "print if @{my $x}=()"


Similarly, 'use strict' should also complain
in the case below since there's no lvalue:

perl -Mstrict -wle "print for @{my $x}"

Quote from the reply Eric Brine wrote [1]

Just like the argument list of sub calls, The list over which
foreach iterates is evaluated in lvalue context as required by
foreach's aliasing property.

This also means that since 5.10 (?):

perl -e 'use strict; use warnings; sub foo { my @y=@_ }; foo( @{my $x} )'

Doesn't result in:
Can't use an undefined value as an ARRAY reference at -e line 1.

While I see now somewhat the logic behind it on the other hand I am
afraid that this will result in very subtle bugs. I am not a new to
Perl, yet I now and then manage to make mistakes like:

exists $x->{ foo }{ bar } or ...

and overlooking that I just created a key foo.

[1] http://rt.perl.org/rt3/Ticket/Display.html?id=89024
 
C

C.DeRykus

But the case above is an lvalue so $x autovivifies.
In the other example though:
   perl  -Mstrict -wle "print if @{my $x}"
'strict' generates a "Can't use an undefined
value as an ARRAY reference" and autoviv will
occur only without 'strict'.
Make the above an lvalue and there's no error:
   perl  -Mstrict -wle "print if @{my $x}=()"
Similarly, 'use strict' should also complain
in the case below since there's no lvalue:
   perl  -Mstrict -wle "print for @{my $x}"

Quote from the reply Eric Brine wrote [1]

    Just like the argument list of sub calls, The list over which
    foreach iterates is evaluated in lvalue context as required by
    foreach's aliasing property.

This also means that since 5.10 (?):

perl -e 'use strict; use warnings; sub foo { my @y=@_ }; foo( @{my $x} )'

Doesn't result in:
Can't use an undefined value as an ARRAY reference at -e line 1.

While I see now somewhat the logic behind it on the other hand I am
afraid that this will result in very subtle bugs. I am not a new to
Perl, yet I now and then manage to make mistakes like:

exists $x->{ foo }{ bar } or ...

and overlooking that I just created a key foo.

[1]http://rt.perl.org/rt3/Ticket/Display.html?id=89024

Thanks for the cite... that makes sense now.
 
P

Peter J. Holzer

While I see now somewhat the logic behind it on the other hand I am
afraid that this will result in very subtle bugs. I am not a new to
Perl, yet I now and then manage to make mistakes like:

exists $x->{ foo }{ bar } or ...

and overlooking that I just created a key foo.

no autovivification qw(exists);

may help.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top