Re: Detecting filehandles
- From: "Matt Garrish" <matthew.garrish@xxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 08:04:59 -0400
"Kevin Michael Vail" <kevin@xxxxxxxxxx> wrote in message
news:kevin-A5F7F3.21594228042006@xxxxxxxxxxxxxxxxxxx
In article <48mdnTde3IAuLM_ZnZ2dnUVZ_s6dnZ2d@xxxxxxxxxxxx>,
"Subotai" <subotai@xxxxxxx> 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
}
Look at the 'openhandle' function in Scalar::Util.
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.
I don't believe there is, either. It would be difficult to impossible
to implement under Unix, because a file is one thing and its name is
something else; in fact a file can have multiple names (directory
entries), so how would you choose which one is "the" file?
No, but no reason you can't just use a hash to store all the information
about the file and then pass that into the subroutine:
[untested]
my %fileinfo = (filepath => '/somefile.dat');
open ($fileinfo{'filehandle'}, '<', '$fileinfo{'filepath'}) or die $!;
mySub(\%fileinfo);
sub mySub {
my ($arg1) = @_;
if (ref($arg1) eq 'HASH' and defined($arg1->{'filehandle'}) {
# have a filehandle
}
else {
# do not have a filehandle
}
}
Matt
.
- References:
- Detecting filehandles
- From: Subotai
- Re: Detecting filehandles
- From: Kevin Michael Vail
- Detecting filehandles
- Prev by Date: Re: using variable in search and replace
- Next by Date: Re: Detecting filehandles
- Previous by thread: Re: Detecting filehandles
- Next by thread: Re: Detecting filehandles
- Index(es):
Relevant Pages
|