Can I find a filename from a filehandle in Perl?

后端 未结 3 871
逝去的感伤
逝去的感伤 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:19

    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");
    

提交回复
热议问题