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:

#!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 ); } }
And the Extra module is here:
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.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.


In reply to SOLVED: Looking for suitable spells to get open to return a filehandle from a module by talexb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.