Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Getting filehandle access modes on Windows

by djerius (Beadle)
on Sep 28, 2012 at 20:40 UTC ( #996289=perlquestion: print w/ replies, xml ) Need Help??
djerius has asked for the wisdom of the Perl Monks concerning the following question:

I've just released a module to cpan (IO::ReStoreFH) which stores and restores filehandles and file descriptors. (I'm using it to sandbox code which might mess up the main program's I/O).

It's failing on Windows (thanks, CPAN Testers!) because apparently fcntl on that platform can't be used to determine filehandle access modes. Those are needed to dup them using open according to the documentation:

You may also, in the Bourne shell tradition, specify an EXPR beginning with '>&', in which case the rest of the string is interpreted as the name of a filehandle (or file descriptor, if numeric) to be duped (as dup(2)) and opened. You may use "&" after ">", ">>", "<", "+>", "+>>", and "+<". The mode you specify should match the mode of the original filehandle.
There doesn't seem to be a clean method for getting that info from a filehandle.
  • I can poke at the underlying file descriptor using fcntl, which works on Linux & Mac OS X, but fails on Windows.
  • I can use PerlIO::get_layers( $fh, details => 1) to get the filehandle flags, but the definition of the flags is in a C header and not available directly in Perl.
  • I could use PerlIO::Layers, which gets me everything I need, but it uses XS and I'd like something that's pure Perl, as installing XS code on Windows boxes is not something I wish on anyone.

Is there another option that I've missed? I don't have any experience on Windows (and no machines on which to test code) so I'm kind of poking around in the dark.

Thanks,

Diab

Comment on Getting filehandle access modes on Windows
Select or Download Code
Re: Getting filehandle access modes on Windows
by BrowserUk (Pope) on Sep 28, 2012 at 20:54 UTC
    apparently fcntl on that platform can't be used to determine filehandle access modes.

    I wrote Win32::Fmode to address that very problem.

    (and syphilis wrapped it over in what he considers a more user-friendly API in FileHandle::Fmode)

    Unfortunately, the underlying method used by both requires XS -- that was the only method I could find for gaining access to the information.

    I'm not guaranteeing there isn't a non-XS method, but I looked pretty hard at the time and didn't find one.

    it uses XS and I'd like something that's pure Perl, as installing XS code on Windows boxes is not something I wish on anyone.

    PPM makes it simple. Far simpler than installing pure perl modules on *nix in fact.

    A whole 10 seconds (via a very slow connection):

    [21:54:11.73] C:\test>ppm install Win32::Fmode Downloading Win32-Fmode-1.0.6...done Unpacking Win32-Fmode-1.0.6...done Generating HTML for Win32-Fmode-1.0.6...done Updating files in site area...done 6 files installed [21:54:20.57] C:\test>

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      I wrote Win32::Fmode to address that very problem. (and syphilis wrapped it over in what he considers a more user-friendly API in FileHandle::Fmode)
      Thanks! I missed that. All I found was Win32API::File, which exposes file attributes, but not the filehandle ones.

      PPM makes it simple. Far simpler than installing pure perl modules on *nix in fact.
      Good to know. I won't shy away from using XS then.

      Thanks again.

Re: Getting filehandle access modes on Windows
by CountZero (Bishop) on Sep 29, 2012 at 06:18 UTC
    I'd like something that's pure Perl, as installing XS code on Windows boxes is not something I wish on anyone
    Starwberry Perl makes compiling and installing XS code easy and nowadays even ActiveState Perl comes with a full compiler suite.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      That's good to know. My only knowledge of Perl on Windows has been through following mailing list traffic of struggles to get things to compile with various Windows compilers. It seemed that if experienced developers were having struggles that might prove even more problematic for ordinary users. Maybe I follow the wrong lists.
        Just try it for yourself and you will see if it works for you too.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996289]
Approved by hominid
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-12-27 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls