Re: Detecting filehandles
- From: "John W. Krahn" <someone@xxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 12:05:16 GMT
Subotai wrote:
Is there a way to detect if an argument you are passed in a subroutine is a
filehandle? For instance:
open(FILE, '/file');
foo(\*FILE);
foo($someotherarg);
sub foo {
$arg = shift;
#### do some check on $arg to see if it is a filehandle or a normal scalar
}
Use fileno() for that, if it is defined then it is a valid filehandle.
Also if you have an open filehandle, is there a way to see what file it is
referencing? For instance if you open the file "/foo", is there anyway to
get "/foo" from the filehandle? I know with the fileno() call you can get
the file descriptor, is there anyway you could use that to lookup that
information? I don't think there is but I want to be 100% sure.
There is if you have a /proc file system that supports it:
$ perl -le'
open my $fh, q[test.txt] or die $!;
my $fn = fileno $fh;
print readlink "/proc/self/fd/$fn";
'
/home/john/test/test.txt
Or use the PID:
$ perl -le'
open my $fh, q[test.txt] or die $!;
my $fn = fileno $fh;
print readlink "/proc/$$/fd/$fn";
'
/home/john/test/test.txt
John
--
use Perl;
program
fulfillment
.
- References:
- Detecting filehandles
- From: Subotai
- Detecting filehandles
- Prev by Date: Re: Detecting filehandles
- Next by Date: Re: Need help with grep function
- Previous by thread: Re: Detecting filehandles
- Next by thread: Re: Detecting filehandles
- Index(es):
Relevant Pages
|