Re: Odd (undocumented?) behavior of RAM file within a loop
- From: Ben Morrow <ben@xxxxxxxxxxxx>
- Date: Wed, 30 Jan 2008 02:11:20 +0000
Quoth "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>:
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)
This seems to be an bug in PerlIO::scalar. There is a check in scalar.xs
that is supposed to avoid this warning; however, the code checks
SvTYPE(sv) > SVt_NULL ('this scalar has never been allocated') rather
than SvOK(sv) ('this scalar has a defined value'). When a lexical is
reused the former is true but the latter is false, so the warning gets
through. This means that
my $x = 3;
undef $x;
open my $F, '>', \$x;
will warn as well, which it also (IIUC) shouldn't.
I've done up a patch, which I'll send to p5p when I've tested it.
With the close statement commented out, I get:
C:\Temp> t
Pass #1
SCALAR(0x182b04c)
Pass #2
SCALAR(0x22ae14)
This makes less sense... I suspect what's happening here is that the
filehandle isn't being closed until *after* $buffer gets allocated for
the next iteration, so it is forced to allocate a new scalar since the
old one's still in use. This is arguably wrong, since the whole point is
to reuse the old scalar if we can, for efficiency.
Ben
.
- 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
- References:
- Odd (undocumented?) behavior of RAM file within a loop
- From: David Filmer
- Re: Odd (undocumented?) behavior of RAM file within a loop
- From: A. Sinan Unur
- Odd (undocumented?) behavior of RAM file within a loop
- Prev by Date: FAQ 8.40 How do I avoid zombies on a Unix system?
- Next by Date: Re: List of directories within a directory
- 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
|