sfpug_perl_passing_file_handles

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: Paul Bindels <pbindels@looksmart.net>
From: David Lowe <dlowe@pootpoot.com>
Subject: Re: [sf-perl] IO::Poll question
Date: Thu, 7 Jun 2001 17:04:50 -0700 (PDT)

On Thu, 7 Jun 2001, Paul Bindels wrote:

> I'm having a problem using the IO::Poll module and I'm not having much
> luck finding any examples on the web.  I wrote this program in
> C without a problem but would like to have it in Perl for portability. 
> 
> Basically, I am opening a FIFO with sysopen.  I am assuming that you use
> the filehandle to set the event mask (in C I had to use the file
> descriptor).  When I call the poll method without a timeout value it just
> hangs.  I know it is blocking but it continues to block even when I write
> something to the FIFO that I am polling.
> 
> Also when I call $mypoll->events(INPUT) it always returns an event mask of
> zero.
> 
> 1) Do you pass file handles rather than file descriptors to the mask
> function?  If file descriptor, how do I get the file descriptor for the
> file I opened.
> 
> 2) Am I using the right form to call the events method?
> 
> A code snippet is below.
> 
> Thanks for any info!
> Paul
> 
> ----------------
> use IO::Poll qw(POLLRDNORM POLLWRNORM POLLIN POLLHUP);
> use FileHandle;
> 
> $mypoll = new IO::Poll;
> 
> sysopen INPUT, "./mypipe1", O_RDONLY|O_NONBLOCK;
> 
> open (OUTPUT,">mypipelog") or die "Can't open out";
> 
> $mypoll->mask(INPUT, POLLIN);
> 
> while(1){
>    $str=$mypoll->poll();
>    $i = read INPUT, $string, 1024;
>    print "$i and $string  \n";
> }


Paul et. al. -

You should probably 'use strict' in your code - it would have caught the
problem for you:

  Bareword "INPUT" not allowed while "strict subs" in use...

The problem is, you can't pass a normal perl filehandle the way you just
tried to - you have to pass a glob (yuck!) or encapsulate the filehandle
in some other data type (an object).

IO::Poll expects IO::Handle (or descendant) objects as input (in your
case, an IO::File object).  Try replacing the sysopen call with:

  my $fh = new IO::File "./mypipe1", O_RDONLY|O_NONBLOCK;
  die "unable to open ./mypipe1" if not defined $fh;

And the call to mask:

  $mypoll->mask($fh => POLLIN);

And don't forget 'use strict' and 'use IO::File'!

Hope that helps...

===

the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu