Is this a perl bug?

T

Ted Hopp

Why does the following program print "Undefined!"? Shouldn't the subroutine
leave the value of $x alone? (I can understand it clobbering $_, but $x?)

#!/usr/bin/perl -w
use strict;
sub readZip() {
open FH, '/dev/null';
while (<FH>) { print "Got something!\n"; }
}

my $x = 'hi';
for ($x) { readZip(); }
print "Undefined!\n" unless defined $x;

(I'm using perl 5.6.1)

Thanks,

Ted Hopp
(e-mail address removed)
 
D

dw

Ted Hopp said:
Why does the following program print "Undefined!"? Shouldn't the subroutine
leave the value of $x alone? (I can understand it clobbering $_, but $x?)

#!/usr/bin/perl -w
use strict;
sub readZip() {
open FH, '/dev/null';
while (<FH>) { print "Got something!\n"; }
}

my $x = 'hi';
for ($x) { readZip(); }
print "Undefined!\n" unless defined $x;

for ($x) { .... } makes $_ an alias for $x
inside readZip(), while (<FH>) modifies $_ (which is still an alias for $x)

maybe adding
local $_;
or
my $_;
inside readZip will produce the results you want
 
S

slimzhao

Ted Hopp said:
Why does the following program print "Undefined!"? Shouldn't the subroutine
leave the value of $x alone? (I can understand it clobbering $_, but $x?)

#!/usr/bin/perl -w
use strict;
sub readZip() {
open FH, '/dev/null';
while (<FH>) { print "Got something!\n"; }
}

my $x = 'hi';
for ($x) { readZip(); }
print "Undefined!\n" unless defined $x;

(I'm using perl 5.6.1)

Thanks,

Ted Hopp
(e-mail address removed)

It's not a perl's bug.
for ($x) { readZip(); }
This loop run once, $_ is set to $x, in actually, $_ is an alias of
$x, so if something in the for BLOCK changes $_, $x also changed,
while, now look at your readZip.
while(<FH>) { print "Got something!\n"; }
The loop's BLOCK doesn't run, but <FH> is evaluted once. while(<FH>)
is same as
while($_=<FH>), so, $_ is set to undef, and, the outer $x is set to
undef. That's it.

If you change your while statement to something like:
while(my $line = <FH> ) { .. }
everything is OK.
 
S

slimzhao

Ted Hopp said:
Why does the following program print "Undefined!"? Shouldn't the subroutine
leave the value of $x alone? (I can understand it clobbering $_, but $x?)

#!/usr/bin/perl -w
use strict;
sub readZip() {
open FH, '/dev/null';
while (<FH>) { print "Got something!\n"; }
}

my $x = 'hi';
for ($x) { readZip(); }
print "Undefined!\n" unless defined $x;

(I'm using perl 5.6.1)

Thanks,

Ted Hopp
(e-mail address removed)

the $_ is local in the for BLOCK, so it's visible for all the chained
subroutine in runtime, redeclare $_ as local will break the rules.
sub readZip()
{
local $_; #This will protect the outer $_ well.
...
}

I heard that "There's more than one way to do it", ok, here's the No 2

No.3
change your for statement to:
for $y($x) { ... }
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top