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

Select and file handler

by c_chipster (Acolyte)
on Nov 13, 2005 at 20:24 UTC ( #508141=perlquestion: print w/replies, xml ) Need Help??
c_chipster has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I was reading How do I tell if a handle is open for writing?. Although the thread didn't go down that way, but the guy sort of indicated that select can be used on normal files. I don't think so though. For example, the following code always prints -1 - nothing writable:

use strict; use warnings; use IO::Select; my $file; open $file, ">", "somefile"; my $sel = IO::Select->new(); $sel->add($file); my @writables= $sel->can_write(); print $#writables;

Can anyone demo the opposite?

Replies are listed 'Best First'.
Re: Select and file handler
by pg (Canon) on Nov 13, 2005 at 22:18 UTC

    Looks like that this is not cross-platform. With Windows XP, the OP's code does return -1.

    A note that could be related: on Windows, select() is implemented as part of Windows Socket 2. Most likely, select() only works with socket on Windows.

      select only works on sockets in Win32. Update: I guess you said that already. I apparently didn't read your entire post. Well, take it as confirmation, then.
Re: Select and file handler
by graff (Chancellor) on Nov 13, 2005 at 21:02 UTC
    I ran that code as-is, in two conditions:
    1. (inadvertently) in a directory where I did not have write permission; the result was consistently "-1" as you reported.
    2. (after seeing my initial mistake) in a directory where I did have write permission; the result was "0" -- i.e. there was one element returned by  $sel->can_write(); given that the file had been opened successfully
    (this is on macosx 10.3/perl 5.8.1)

    That'll teach us about forgetting to do error checking -- i.e. "open ... or die ..." would have been prudent.

Re: Select and file handler
by Tanktalus (Canon) on Nov 13, 2005 at 20:53 UTC

    I actually get "0". But that's confusing. I changed the last line to print scalar @writables, $/;, and got "1" as my output. Are you sure that your open succeeded? Try changing the open line to open $file, ">", "somefile" or die "Can't open somefile for write: $!" and see if that tells you anything.

Re: Select and file handler
by gaal (Parson) on Nov 13, 2005 at 20:53 UTC
    What OS/perl are you on? This works for me on linux and 5.8.7.


    1. make sure your IO isn't failing. Add the usual or die guards.
    2. scalar @writables is a more natural way in Perl to look at the size of an array.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://508141]
Approved by friedo
[Corion]: MidLifeXis: Oh, enjoying the show from the sidelines I see...
[MidLifeXis]: I almost think that the coach is benching me. WTH?

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2017-03-23 12:33 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (286 votes). Check out past polls.