Re: Odd (undocumented?) behavior of RAM file within a loop




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

.



Relevant Pages

  • Re: problem interpolating in time/space coordinate
    ... frequent variations in the scalar value. ... Warning: Duplicate x-y data points detected: using average of the z ... amplitude variations away from the original data and the warning: ...
    (comp.soft-sys.matlab)
  • Re: Strange !exists/foreach/read interaction
    ... > Why does read complain about an output-only parameter being undefined? ... Attempts to read LENGTH characters of data into variable SCALAR from ... That could be the cause of the warning. ...
    (comp.lang.perl.misc)
  • free unreferenced scalar error
    ... I have been getting an annoying warning about dereferencing an unreferenced ... present, on my system, I get "Attempt to free unreferenced scalar" using ... I just got through hacking the script on my home Linux box ... is a DBD::ODBC problem or just my own incompetence in duplicating conditions? ...
    (perl.dbi.users)
  • scalar on a populated hash.
    ... I do not understand why scalar is returning me 2/8? ... My goal is to get a count of the values in each hash. ... print "Warning: savecrash hash is empty ...
    (perl.beginners)
  • K3b Error 254---x Cdrecord is not being run with root privileges.
    ... had no problems writing using K3b, and never got an error message about ... x Cdrecord is not being run with root privileges. ... WARNING: Cannot do mlockall. ... /usr/bin/cdrecord: WARNING: This causes a high risk for buffer underruns. ...
    (Fedora)