Odd (undocumented?) behavior of RAM file within a loop
- From: David Filmer <usenet@xxxxxxxxxxxxxxx>
- Date: Tue, 29 Jan 2008 10:53:48 -0800
Greetings. I am writing a program which processes some files within a loop, and I am using the ability (as of Perl 5.8) to open a filehandle on a scalar reference (which I recycle in the loop). Kindly consider this stripped-down trivial code example which illustrates my question:
#!/usr/bin/perl
use strict; use warnings;
foreach (1..3) {
warn "Pass #$_\n";
my $file_in_memory;
open(my $fh, '>', \$file_in_memory) or die "Oops - $!\n";
close $fh;
}
__END__
It works fine for the first pass. However, subsequent passes emit warnings:
Pass #1
Pass #2
Use of uninitialized value in open at /perl/blah line 7.
Pass #3
Use of uninitialized value in open at /perl/blah line 7.
(line 7 is the "open" statement). Sure enough, if I make this change to "initialize" the scalar:
my $file_in_memory = '';
then I have no problems (except I bust the second rule of chapter 4 in Dr. Damian's _PBP_ - yikes).
Although the problem is easy enough to work around, I wonder why it behaves this way (which I don't see documented). The really strange thing (to me) is that it doesn't complain on the first pass. Any ideas?
Thx!
--
David Filmer (http://DavidFilmer.com)
.
- Follow-Ups:
- Re: Odd (undocumented?) behavior of RAM file within a loop
- From: A. Sinan Unur
- Re: Odd (undocumented?) behavior of RAM file within a loop
- From: John Bokma
- Re: Odd (undocumented?) behavior of RAM file within a loop
- Prev by Date: scalar to array?
- Next by Date: Re: scalar to array?
- Previous by thread: scalar to array?
- Next by thread: Re: Odd (undocumented?) behavior of RAM file within a loop
- Index(es):
Relevant Pages
|