http://www.perlmonks.org?node_id=996289

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.

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

Replies are listed 'Best First'.
Re: Getting filehandle access modes on Windows
by BrowserUk (Patriarch) 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