Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to get file descriptor (number)

by Eyck (Priest)
on Sep 13, 2005 at 09:53 UTC ( #491532=perlquestion: print w/ replies, xml ) Need Help??
Eyck has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to provide fd for Linux::INotify, but I'm having problems fetching that..., I tried something like that:

my ($fd); open($fd,"/tmp"); use Data::Dumper; print "PID: $$"; print Dumper(\$fd);
but all I get is:
 PID: 22117
 $VAR1 = \\*{'::$fd'};
Additionally I tried looking at that pid:
wraith eyck 11:17 ~> ls /proc/22117/fd -l
total 0
lrwx------  1 eyck root 64 Sep 13 11:17 0 -> /dev/pts/44
lrwx------  1 eyck root 64 Sep 13 11:17 1 -> /dev/pts/44
lrwx------  1 eyck root 64 Sep 13 11:17 2 -> /dev/pts/44
lr-x------  1 eyck root 64 Sep 13 11:17 3 -> /tmp
Is there a way to see that from inside?

Comment on How to get file descriptor (number)
Download Code
Re: How to get file descriptor (number)
by dave_the_m (Parson) on Sep 13, 2005 at 10:25 UTC
    $ perldoc -f fileno fileno FILEHANDLE Returns the file descriptor for a filehandle, or undefined if the filehandle is not open. This is mainly useful for con- structing bitmaps for "select" and low-level POSIX tty-handling operations. If FILEHANDLE is an expression, the value is taken as an indirect filehandle, generally its name. You can use this to find out whether two handles refer to the same underlying descriptor: if (fileno(THIS) == fileno(THAT)) { print "THIS and THAT are dups\n"; } (Filehandles connected to memory objects via new features of "open" may return undefined even though they are open.)

    Dave.

      Thanks, that is exactly it!
Re: How to get file descriptor (number)
by liverpole (Monsignor) on Sep 13, 2005 at 12:12 UTC
    What dave_the_m wrote is correct, as well as being a great example of using perldoc to read the Perl documentation.  (I use it so much on Linux that I've got "pd" aliased to "perldoc", and "pf" to "perldoc -f").

    However, it looks like you are trying to open a directory ("/tmp") rather than a file.   If that's the case, you can still use a filehandle (I like to create it with FileHandle), but you probably want "readdir" rather than "open":

    use FileHandle; my $fh = new FileHandle; my $dir = "/tmp"; opendir($fh, $dir) or die "Unable to open directory '$dir' ($!)\n" +; my @files = readdir($fh); closedir $fh;
    At the end of this segment of code, the list "@files" will contain the files from the directory "/tmp".  Note that the names will NOT be full pathnames, so if you want that, you'll have to prepend "/tmp" to each member in the list:
    map { $_ = "$dir/$_" } @files; # Prepend "/tmp" to all files
    Another note -- the directories "." and ".." will also be in the list, and you usually want to skip them for any further processing.

    I'll let you try "perldoc -f opendir" and "perldoc -f readdir" if you want to read more about them.

      I would REALLY love an ability to mark this reply as 'OFFTOPIC'
      I use it so much on Linux that I've got "pd" aliased to "perldoc", and "pf" to "perldoc -f"

      Great idea! But, are you implying that you don't have a "pq" alias? ;^).

      --
      David Serrano

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://491532]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-07-12 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls