Re: Odd (undocumented?) behavior of RAM file within a loop
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 29 Jan 2008 20:53:37 GMT
David Filmer <usenet@xxxxxxxxxxxxxxx> wrote in
news:2eOdndq_7J5h6ALaRVn_vwA@xxxxxxxxxxxx:
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:
I found it curious that the warning disappears if I commented out the
close statement (on AS Perl 5.10 on Win32).
So, I looked a little closer:
C:\Temp> cat t.pl
#!/usr/bin/perl
use strict;
use warnings;
for my $pass ( 1 .. 3 ) {
warn "Pass #$pass\n";
open my $fh, '>', \(my $buffer) or die $!;
print \$buffer, "\n";
close $fh or die $!
}
__END__
C:\DOCUME~1\asu1\LOCALS~1\Temp> t
Pass #1
SCALAR(0x182b04c)
Pass #2
Use of uninitialized value $buffer in open at C:\Temp\t.pl line 8.
SCALAR(0x182b04c)
Pass #3
Use of uninitialized value $buffer in open at C:\Temp\t.pl line 8.
SCALAR(0x182b04c)
With the close statement commented out, I get:
C:\Temp> t
Pass #1
SCALAR(0x182b04c)
Pass #2
SCALAR(0x22ae14)
Pass #3
SCALAR(0x22ae24)
Note that the explicit close results in the reference to the same
memory area being reused whereas, without the explicit close, the
reference changes.
I do not know if this behavior is peculiar to my platform or if it
is documented in some way. I would have expected the behavior to be
the same with or without the explicit close.
I also tried this with Cygwin Perl 5.8.8 and got the following:
Without explicit close:
perl t.plPass #1
SCALAR(0x10070f2c)
Pass #2
SCALAR(0x100671c4)
Pass #3
SCALAR(0x10070f2c)
With explicit close:
perl t.plPass #1
SCALAR(0x10070f2c)
Pass #2
Use of uninitialized value in open at t.pl line 8.
SCALAR(0x10070f2c)
Pass #3
Use of uninitialized value in open at t.pl line 8.
SCALAR(0x10070f2c)
Hmmmmm ...
Sinan
--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>
.
- Follow-Ups:
- Re: Odd (undocumented?) behavior of RAM file within a loop
- From: Ben Morrow
- Re: Odd (undocumented?) behavior of RAM file within a loop
- References:
- Odd (undocumented?) behavior of RAM file within a loop
- From: David Filmer
- Odd (undocumented?) behavior of RAM file within a loop
- Prev by Date: domains, top level, collecting a list of
- Next by Date: Re: domains, top level, collecting a list of
- Previous by thread: Re: Odd (undocumented?) behavior of RAM file within a loop
- Next by thread: Re: Odd (undocumented?) behavior of RAM file within a loop
- Index(es):
Relevant Pages
|