happi has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to capture data from a linux device file; in the form:

#!/../../perl -w open(FF, "< /dev/mouse") || die "cannot open handle \n"; while (<FF>) { $vr = $_; print $vr; } close (FF);
------------------------------

I keep getting the error "readline() on closed filehandle FF on line 3"

I've tried this with /dev/ttyS1 , /dev/usb/USB/scanner0 and /dev/fd0

This works fine if I use a test file, like :

open(FF, "< /path/to/file.txt" );
Also, I tried this in the form: sysopen(FF, "/dev/fd0", O_RDONLY); but I always get the error that says "item O_RDONLY isn't numeric at line 3"

Why doen't the sysopen work ? why does the device file always seem closed or non-existent ?

I am using Linux 9 and Perl 5.8 . I would hope not to need any Perl modules for something this basic... Help Help..... Happi..

Edited by Chady -- code tags and minor formatting.

Replies are listed 'Best First'.
Re: Linux device filehandle roadblock
by amw1 (Friar) on Jun 11, 2004 at 18:42 UTC
    this appears to work:

    or at least stuff is output. I've never used the POSIX::read/open stuff before so there may be holes I didn't catch.

    did the sprintf stuff to get the hex stream rather than unprintable garbage. May not work the way I think it does. :)
    + use strict; my $data; my $fd; my $buf; use POSIX; $fd = POSIX::open("/dev/mouse", &POSIX::O_RDONLY) || die ($!); while($data = POSIX::read($fd, $buf, 1)) { print "READ $data Bytes: " . join("", map(sprintf("%x",ord($_)), sp +lit(//, $buf))) . "\n"; }
Re: Linux device filehandle roadblock
by pbeckingham (Parson) on Jun 11, 2004 at 18:09 UTC

    You need a numeric equivalent to O_RDONLY, from the <sys/fcntl.h>, which has the value 0 on my sytem.

      Or you could do the correct thing and use the value provided by Fcntl and be right on anybody's system.

      Thanks, I'll investigate this.. :)
Re: Linux device filehandle roadblock
by zentara (Cardinal) on Jun 12, 2004 at 15:24 UTC
    This is just a WAG, but I remember playing around with something similar awile back, and had some luck by rewinding the file handle after every read.

    Something like (totally pseudo-code):

    sysopen FH, /dev/fd0, O_RDWR|O_CREAT,0666 or die "Cannot open : $!"; while(1){ seek FH, 0, SEEK_SET or die "Cannot rewind $file: $!"; my $data = <FH>; #truncate FH, 0 print "$data\n"; }
    The error "error that says "item O_RDONLY isn't numeric at line 3" may be caused by not having "use Fnctl;" at the top.

    I'm not really a human, but I play one on earth. flash japh