Can I find a filename from a filehandle in Perl?
Asked Answered
L

3

23
open(my $fh, '>', $path) || die $!;
my_sub($fh);

Can my_sub() somehow extrapolate $path from $fh?

Loaf answered 11/5, 2010 at 17:48 Comment(0)
T
17

A filehandle might not even be connected to a file but instead to a network socket or a pipe hooked to the standard output of a child process.

If you want to associate handles with paths your code opens, use a hash and the fileno operator, e.g.,

my %fileno2path;

sub myopen {
  my($path) = @_;

  open my $fh, "<", $path or die "$0: open: $!";

  $fileno2path{fileno $fh} = $path;
  $fh;
}

sub myclose {
  my($fh) = @_;
  delete $fileno2path{fileno $fh};
  close $fh or warn "$0: close: $!";
}

sub path {
  my($fh) = @_;
  $fileno2path{fileno $fh};
}
Tallowy answered 11/5, 2010 at 17:56 Comment(1)
Thanks. Not what I wanted to hear but the more efficient workaround.Loaf
L
8

Whoever might be looking for better way to find the file name from filehandle or file descriptor:

I would prefer to use the find -inum , if available. Or, how about using following way, always - any drawbacks except the unix/linux compatible!

my $filename='/tmp/tmp.txt';
open my $fh, '>', $filename;
my $fd = fileno $fh;
print readlink("/proc/$$/fd/$fd");
Lettering answered 25/6, 2012 at 21:18 Comment(2)
You can also use fileno ${^LAST_FH} if you don't have the filehandle handy (e.g. you're using <>).Hermes
This is Linux-compatible, not generally Unix-compatible.Gullet
C
6

You can call stat or IO::Handle::stat on a filehandle -- that will give you the device and inode of the file that you have opened. With that and a little operating system wizardry you can find the filename. OK, maybe a lot of operating system wizardry.


The find command has an -inum option to find a file with a specified inode number. This is probably not going to be as efficient as caching the path when you open the file, as gbacon recommends.

Crowns answered 11/5, 2010 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.