Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Test for writable filehandles on win32

by tsee (Curate)
on Jun 09, 2008 at 14:48 UTC ( #691038=perlquestion: print w/replies, xml ) Need Help??
tsee has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm a user of the DBM::Deep module. That used to have only pure-Perl dependencies outside of the Perl core, but recently, a dependency on an XS module to test whether a given filehandle is writable. This is a problem for me since I'm actually using DBM::Deep in a bootstrapping environment which is used for installing binaries of XS modules which the host can't compile (think PAR).

Now, if you look at this RT ticket for DBM::Deep, that suggests that this dependency could potentially be removed.

The pure-Perl alternative suggested in that ticket should work for *nix, but will probably leave win32 unsolved. I can't test the syswrite writability test for lack of a Windows.

Now, my question is: Can one of those who develop on Windows test that syswrite-based solution? If it works, does it still work without actually writing data to the filehandle? Does anybody have an idea how to fix this entirely?

Thanks in advance for any ideas and help.

Best regards,

P.S.: Before somebody suggests that I have to bring it up to the DBM::Deep author(s) instead of posting here: I did. They're sympathetic with my cause, but can't help for lack of time. See the DBM::Deep Googlegroups group.

Replies are listed 'Best First'.
Re: Test for writable filehandles on win32
by tachyon-II (Chaplain) on Jun 09, 2008 at 16:52 UTC

    Given that the bug report suggests only the is_W() function is used from FileHandle::Fmode I don't see why you can't replace the entire dependency with this short sub:

    sub is_W { my $fh = shift; return 0 unless defined $fh; return 0 unless defined fileno $fh; local $\ = ''; # just in case no warnings; # temporarily disable warnings #my ($atime,$mtime) = (stat($fh))[8,9]; my $is_w = print $fh ''; #eval{ utime $atime, $mtime, $fh }; # can fail if futimes not avai +lable return $is_w; } my $file = "C:/tmp.txt"; printf "Null fh %d\n", is_W(); open F, ">", $file or die "Can't create $file $!\n"; printf "Writable fh %d\n", is_W(*F); close F; open F, $file or die "Can't create $file $!\n"; printf "Readable fh %d\n", is_W(*F); close F; printf "STDIN %d\n", is_W(*STDIN); printf "STDOUT %d\n", is_W(*STDOUT); printf "STDERR %d\n", is_W(*STDERR); __DATA__ Null fh 0 Writable fh 1 Readable fh 0 STDIN 0 STDOUT 1 STDERR 1

    Update added "no warnings;" as per barts suggestion. Added comments containing mtime restoration code (if desired) also per bart.

      Is it really that simple?? That looks like it would be completely platform-independent ...

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        It probably will work... but it'll change the file modification time, if it succeeds. This probably isn't what is wanted, but it may not matter if you're planning on writing to the filehandle anyway.
      If you don't disable warnings for that sub, you get lots of warnings like this one:
      Filehandle F opened only for input at ...
Re: Test for writable filehandles on win32
by dragonchild (Archbishop) on Jun 09, 2008 at 21:05 UTC
    I have just released 1.0012 which contains tachyon-II's fix.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Thanks to everybody who contributed to this thread and in particular tachyon-II for the fix and dragonchild for quickly applying it. Your help is much appreciated!

      Best regards,
      (Update: fix HTML)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://691038]
Approved by Corion
[Lotus1]: now I'll be sent to re-education camp or something
[stevieb]: relocate Lotus1 to the brig, straight away!
[erix]: let's do an education simulation
[stevieb]: You could just play dumb... "huh? There was no mention of 'fishing' in the email!"
[erix]: you could say you were part of the simulation
[stevieb]: I'd take full responsibility myself... "man, I was so hung over I could barely read what the email said!"
[zarath]: "Smee, the boo-box..."

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2017-05-24 13:45 GMT
Find Nodes?
    Voting Booth?