while() doesn't localize $_ but for() does in some situations. Isthis expected?

M

Matthew Horsfall

It seems that while() loops don't localize $_ in certain situations,
whereas for loops do.

The following works fine:

$ echo Test | perl -e 'for (qw(Word)) { for (<STDIN>) { print "$_";
exit;} }'
Test

Whereas the following crashes:

$ echo Test | perl -e 'for (qw(Word)) { while (<STDIN>) { print "$_";
exit;} }'
Modification of a read-only value attempted at -e line 1.

Is this expected behavior?

I will note this only seems to happen when modifying $_ using <>
 
W

Willem

Matthew Horsfall wrote:
) It seems that while() loops don't localize $_ in certain situations,
) whereas for loops do.

For loops don't *localize* $_ either.
What they do is *alias* $_ to whatever they're looping on.

Try:
sub foo { print } for (<STDIN>) { foo }
As opposed to:
sub foo { print } my $_; for (<STDIN>) { foo }

) Is this expected behavior?

Yes. for() aliases whereas while() assigns.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
R

Randal L. Schwartz

Willem> For loops don't *localize* $_ either.

foreach loops do. for loops don't.

See the following:

my $x = 5;
foreach $x (10..15) {
print $x, "\n"; # prints 10..15
}
print $x, "\n"; # prints 5.

Clearly, $x "inside" the foreach loop is a different $x from the
outside.

print "Just another Perl hacker,"; # the original
 
S

sln

It seems that while() loops don't localize $_ in certain situations,
whereas for loops do.

The following works fine:

$ echo Test | perl -e 'for (qw(Word)) { for (<STDIN>) { print "$_";
exit;} }'
Test

Here said:
Whereas the following crashes:

$ echo Test | perl -e 'for (qw(Word)) { while (<STDIN>) { print "$_";
exit;} }'
Modification of a read-only value attempted at -e line 1.

But you don't mention if you want to see "Word" printed or what
Is this expected behavior?

I will note this only seems to happen when modifying $_ using <>

No, it happens anytime you try to write to a constant:
$_ = "modify" for (qw(Word));

while() has a special property, only the first assignable item in the expression will
do an asignment to the default variable (the one you have aliased to a constant).

So (untested),
$ echo Test | perl -e 'for (qw(Word)) { while ($_ and <STDIN>) { print "$_";
would work but it won't print out what you think it will.
Or,
for (qw(Word)) {
while ($_ && <STDIN>) {
print $_,"\n";
}
}

Where above, <STDIN> is not assigned to anything.
On the other hand,
while (9 && <STDIN>)
in the above context wil produce that error.


-sln
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top