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



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.pl
Pass #1
SCALAR(0x10070f2c)
Pass #2
SCALAR(0x100671c4)
Pass #3
SCALAR(0x10070f2c)

With explicit close:

perl t.pl
Pass #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>

.



Relevant Pages

  • Regarding copy constructors and mutators
    ... One thing that confused me about Perl (no matter how often I read ... "perldoc overload" page and in the Camel book explain that overloading ... as a reference to an object or as an actual copy of an object. ... time for another clarification: ...
    (comp.lang.perl.misc)
  • Re: Abstract public member variales?
    ... every object that has ever come out of the factory persistent, ... Usually that is trivial because to pass the reference the message sender needs the reference in hand and it shouldn't have access to it after the object is deleted. ... GC causes more problems than it is worth and to be safe one should always invoke an explicit 'delete' method when an object's life cycle is done regardless of whether the language provides GC or not. ... since an object that is no longer useful can likely be safely deleted from the set of domain objects (ignoring persistence). ...
    (comp.object)
  • Re: Question on LSP
    ... not have to be explicit attributes, ... Since objects in memory usually don't move, the language can largely hide the identity mapping. ... My issue here is that whatever semantic meta model the language uses, there must be some way for the developer to unambiguously express the OOA/D is-a semantics for some problem space entity. ... Note that the language allows us to use a name like 'T' on the reference as a mnemonic so that the developer can keep track of what is happening with the indirection. ...
    (comp.object)
  • Re: associate + attach?
    ... typeglobs seem rather odd from my perspective, ... reference, too. ... However Perl "file handles" are a special case. ... "typeglob" entity that contains the filehandle. ...
    (comp.lang.perl.misc)
  • Re: taking references to functions
    ... > I'm building a diagnostic medical questionnaire using Perl Tk. ... > to create a reference to a named function but not for unnamed ones. ... Notice that %dispatch here is an actual hash variable. ...
    (perl.beginners)