in reply to retrieve filename from filehandle?

zentara told you how to get the file descriptor of your handle. Now, under Linux you can get a symlink to your original file from

/proc/self/fd/$descriptor

I don't know how portable this is to other unices, not to say osen - generally I expect little to zero. Still it may be suitable for you!

Replies are listed 'Best First'.
Re^2: retrieve filename from filehandle?
by tirwhan (Abbot) on Jan 16, 2006 at 19:35 UTC

    A slightly more cross-platform way of doing this (works on most Unixes) would be to use lsof. The following will first determine the file descriptor from the filehandle (as per zentara's example) and then uses this file descriptor to find the file name.

    my $desc = fileno($handle); my $name = (`lsof -Fn -a -d $desc -p $$`)[1]; $name = substr($name,1);

    There are ten types of people: those that understand binary and those that don't.
Re^2: retrieve filename from filehandle?
by Arunbear (Prior) on Aug 09, 2007 at 09:16 UTC
    Thank you very much for that tip! Here is an example of how it could be used:
    arun@butterfly:~> perl -de 0 Loading DB routines from perl5db.pl version 1.23 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 0 DB<1> open $fh, '>', 'temp.txt' DB<2> x $fh 0 GLOB(0x816b940) -> *main::$fh FileHandle({*main::$fh}) => fileno(6) DB<3> $d = fileno $fh DB<4> p $d 6 DB<5> p qx[ls /proc/$$/fd] 0 1 2 3 4 5 6 7 DB<6> p readlink("/proc/$$/fd/$d") /home/arun/temp.txt