Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

reverse engineer a filehandle

by smile4me (Beadle)
on Dec 09, 2011 at 22:29 UTC ( #942740=perlquestion: print w/replies, xml ) Need Help??

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

According to http://perldoc.perl.org/perlopentut.html: "Indirect filehandles also make it easy to pass filehandles to and return filehandles from subroutines."

Okay, so how do I find out which file this filehandle references? If the file name is dynamically generated, and all I have is a filehandle, how do I know which file is being written into?

For example:
#get a filehandle $fh = get_fh_from_file( generate_filename() ); if ( print_data( $fh, $data ) ) { say "data printed to file: $fh"; } ## got: data printed to file: *TRSC::FileUtils::FH; ## expected: '/path/to/file'
Thanks,
Steve

Replies are listed 'Best First'.
Re: reverse engineer a filehandle
by tobyink (Canon) on Dec 09, 2011 at 23:17 UTC

    As the first answer suggested, your best bet is to make sure you store the file name at the same time you open the filehandle.

    Failing that, this works on Linux, but is very non-portable:

    open my $fh, '/etc/motd'; my $fd = fileno($fh); print readlink("/proc/$$/fd/$fd");
      Thanks! the "readlink("/proc/$$/fd/$fd");" is what I was looking for.
Re: reverse engineer a filehandle
by TJPride (Pilgrim) on Dec 09, 2011 at 22:55 UTC
    Best way is to store a hash of both the filehandle and its name:

    use strict; use warnings; use Data::Dumper; sub openRandom { my $fh = { name => (int rand 1000) . '.txt' }; return $fh if open($fh->{'handle'}, ">$fh->{'name'}"); } print Dumper(openRandom());
Re: reverse engineer a filehandle
by JavaFan (Canon) on Dec 10, 2011 at 18:47 UTC
    "Indirect filehandles also make it easy to pass filehandles to and return filehandles from subroutines."

    Okay, so how do I find out which file this filehandle references?

    They way you pose the question makes me think the latter should follow from the former.

    I think how easy it is to pass around filehandles is complete unrelated to finding out file names. Do note you question may not have a unique answer:

    • A "file"handle may actually read or write from a socket. Or a pipe.
    • The file handle may be STDIN, STDOUT or STDERR.
    • Or it may be some other redirection.
    • Even if the file handle accesses a real file, the file may not have a name.
    • Or the file may have more than one name.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2021-06-12 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (53 votes). Check out past polls.

    Notices?