talexb has asked for the wisdom of the Perl Monks concerning the following question:
I'm working on what I imagined would be a simple module to provide versioned access to files (this is something that VMS provides), but it seems I've run up against a challenging (to me) situation with opening files in a module, and passing the file handle back to the caller as open does.
Here's some code that demonstrates the problem:
And the Extra module is here:#!perl use strict; use warnings; use Extra; { { open ( my $fh, '>', 'normal-open.txt' ) or die "1. Open failed: $!"; defined $fh or die "1. Filehandle not defined"; print $fh "Normal open works fine.\n"; close ( $fh ); } { Extra::open ( my $fh, '>', 'extra-open.txt' ) or die "2. Open failed: $!"; defined $fh or die "2. Filehandle not defined"; print $fh "Extra open works fine.\n"; close ( $fh ); } }
package Extra; use Symbol; sub open { my ( $fh, $direction, $filename ) = @_; $fh = Symbol::gensym; open ( $fh, $direction, $filename ); } 1;
The first block works fine, and the second block returns from, but the filehandle is undefined.
I had a good look at the code for File::Temp, and saw where it uses the call to Symbol::gensym for Perls before 5.6, so I tried adding that in. It makes no difference -- I'm using Perl 5.22.1 anyway.
My plan is to hook the call to open and seamlessly handle open a versioned file using the requested file name as a basis -- I've also tried using sysopen, without any success. There's some magic going on that I don't understand.
|
---|