in reply to Retriving Filename from Filehandle

I'm not aware that it is possible. I have build hashrefs in the past to keep the two pieces of information together. In the simplest case, it looks something like this (warning, untested code ahead):

use IO::File; my $file; $file->{name} = '/tmp/foo'; $file->{fd} = IO::File->new( $file->{name} ) or die "$!\n"; # TODO: +more explicit error message # this lets me do stuff like $file->{fd}->print( "The name of this file is $file->{name}\n" ); $file->{fd}->close();

But that means that you have to start passing these hashrefs around, instead of raw IO::File objects, so that may mean major surgery on your code.

And now I will probably be tarred and feathered for not knowing that the IO::File or one of its ancestors has some really useful method, like name() or something.

--
g r i n d e r
just another bofh

print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';

Replies are listed 'Best First'.
Re: Re: Retriving Filename from Filehandle
by runrig (Abbot) on Dec 18, 2001 at 22:56 UTC
    You could just write a subclass of IO::File which keeps track of this stuff for you. Since an IO::File object is a glob reference, you can stuff the additional info in the hash portion of the glob (I believe...).
Re^2: Retriving Filename from Filehandle
by freonpsandoz (Beadle) on Jan 28, 2019 at 02:02 UTC

    It's really a shame that a filehandle can't be supplied as the filename parameter to open(). As it is, many (most?) Perl modules don't work correctly under Windows because their interfaces use filename parameters and can't handle Unicode filenames.

      So, behaving as if the mode was <& instead of < when a handle is provided? Interesting. You could submit the idea to the Perl devs (perl5-porters@perl.org).