Can I find a filename from a filehandle in Perl?

后端 未结 3 881
逝去的感伤
逝去的感伤 2020-12-10 01:53
open(my $fh, \'>\', $path) || die $!;
my_sub($fh);

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

3条回答
  •  隐瞒了意图╮
    2020-12-10 02:40

    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.

提交回复
热议问题