redo undefines loop variable

C

Chris Charley

A small piece of code (below) to demonstrate the bug.
If the code would run without a bug, it would be an infinite loop.
But the bug produces warnings about the use of an uninitialized value. . .

#####################################################################
################### code #######################################

#!/usr/bin/perl
use strict;
use warnings;

while (my $data = <DATA>) {
print $data;
redo if $data == 2;
}
__DATA__
1
2
3
4


################### end code ##################################
#####################################################################


Note: I also found mention of this bug in a Google search in Groups,
(with demonstration of the bug).

############################################
Message 1 in thread #
From: Bart Lateur ([email protected])
Subject: redo and scope bug #

#
View this article only
Newsgroups: comp.lang.perl.misc #
Date: 2002-01-25 03:49:38 PST
############################################

I tried to use the perlbug program to submit the report, but I need
Mail::Send and can't find it in any of Active State's repositories.

Chris
 
J

Jay Tilton

(e-mail address removed) (Chris Charley) wrote:

: A small piece of code (below) to demonstrate the bug.
: If the code would run without a bug, it would be an infinite loop.
: But the bug produces warnings about the use of an uninitialized value. . .
:
: #!/usr/bin/perl
: use strict;
: use warnings;
:
: while (my $data = <DATA>) {
: print $data;
: redo if $data == 2;
: }
: __DATA__
: 1
: 2
: 3
: 4

As a workaround, you could add another set of curlies to create an interior
block that the redo() will re-execute without clobbering $data.

while (my $data = <DATA>) {{
print $data;
redo if $data == 2;
}}

It probably deserves an explanatory comment so future maintainers of your
code will know what's going on and won't bork it up.

: Note: I also found mention of this bug in a Google search in Groups,
: (with demonstration of the bug).

Nice detective work.

: ############################################
: Message 1 in thread #
: From: Bart Lateur ([email protected])
: Subject: redo and scope bug #
:
: #
: View this article only
: Newsgroups: comp.lang.perl.misc #
: Date: 2002-01-25 03:49:38 PST
: ############################################

The article's Message-ID header is a more convenient way to cite a specific
article or thread. That one is:
Message-ID: <[email protected]>

: I tried to use the perlbug program to submit the report, but I need
: Mail::Send and can't find it in any of Active State's repositories.

You might first check the bug tracker at http://rt.perl.org/perlbug/ to see
if it was submitted back then and what its disposition was.
 
T

Tad McClellan

zentara said:
1.) Wouldn't you need to chomp $data? Otherwise $data will
never == 2.


Run this...

--------------------------------
#!/usr/bin/perl
use strict;
use warnings;

my $data = "2\n";

if ( $data == 2 )
{ print "data is two\n" }
else
{ print "data is NOT two\n" }
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I don't think it's a bug.
I'm no expert on Perl internals, but when I first looked at this,
I had these thoughts.
1.) Wouldn't you need to chomp $data? Otherwise $data will
never == 2.

Try it:
my $var = "2\n";
print $var==2? "Equals 2!\n" : "Isn't 2!\n";
$var = "2foofoolalala";
print $var==2? "Equals 2!\n" : "Isn't 2!\n";

(The latter does issue a warning, however).
2. This seems to work as I would expect since data is a filehandle.
When I run the code with a chomp $data, it prints 1234. It
prints 12, then with the redo, it prints 34. I think you are
assuming
that redo resets the DATA filehandle?

Did you expect the "Use of uninitialized value" warnings? The OP
didn't say (shame on him), but I think that's the "bug" he was
referring to.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE/3JO2Y96i4h5M0egRAtKlAJ91StgHeT7RN58TaIWlopzsLMB8KQCfXuM9
+UkS6rzLHTG0PFcc+sxmfE4=
=bqWv
-----END PGP SIGNATURE-----
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Did you expect the "Use of uninitialized value" warnings? The OP
didn't say (shame on him), but I think that's the "bug" he was
referring to.

My mistake -- Chris *did* state that that was the unexpected
behavior. Shame on me for not re-reading it before posting.

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE/3JQgY96i4h5M0egRAmvAAKDqB+gYtQ4C7iH6vik/UnzfEAhz6wCfWFIY
eVqm0SsZXFmEvX85fLahKkk=
=EawU
-----END PGP SIGNATURE-----
 
K

Ketil

I don't think it's a bug.
I'm no expert on Perl internals, but when I first looked at this,
I had these thoughts.
1.) Wouldn't you need to chomp $data? Otherwise $data will
never == 2.
2. This seems to work as I would expect since data is a filehandle.
When I run the code with a chomp $data, it prints 1234. It prints
12, then with the redo, it prints 34. I think you are assuming
that redo resets the DATA filehandle?

If I change redo to "seek DATA,0,0" you get an infinite loop.

#!/usr/bin/perl
use strict;
use warnings;

while (my $data = <DATA>) {
chomp $data;
print $data;
#redo if $data == 2;
seek DATA, 0, 0 if $data == 2;
}
__DATA__
1
2
3
4
 

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,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top