Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: ref to read-only alias ... why?

by tobyink (Canon)
on Jan 05, 2012 at 22:47 UTC ( [id://946489]=note: print w/replies, xml ) Need Help??


in reply to ref to read-only alias ... why?

From perlsyn's section on foreach loops:

If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail.

What that's saying is that given:

use 5.010; use Data::Dumper; my $foo = "world"; my $bar = "Hello planet"; my $iter = 0; foreach my $x ((1, $foo, substr($bar,6))) { local $@ = undef; printf "Iteration %d:\n", ++$iter; eval { $x = "World" } or say $@; } print Dumper [$foo, $bar];

The first iteration will fail as it's essentially trying to assign 1 = "Hello"; the other iterations will succeed because $foo and substr($bar,6) are both lvalues, so can be assigned to.

In your case, $_ is an alias for the constant 1, not an lvalue, so can't be assigned to.

Replies are listed 'Best First'.
Re^2: ref to read-only alias ... why?
by Eliya (Vicar) on Jan 05, 2012 at 23:28 UTC
    In your case, $_ is an alias for the constant 1, not an lvalue, so can't be assigned to.

    This doesn't really answer the OP's question why it does not fail when you try to modify the constant via a reference to the alias.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://946489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-28 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found