Re: Detecting filehandles
- From: Tad McClellan <tadmc@xxxxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 09:06:57 -0500
Subotai <subotai@xxxxxxx> wrote:
Is there a way to detect if an argument you are passed in a subroutine is a
filehandle?
No, because it is not possible to pass a filehandle as a
subroutine argument.
For instance:
open(FILE, '/file');
foo(\*FILE);
That is not passing a filehandle.
That is passing a reference to a typeglob.
foo($someotherarg);
sub foo {
$arg = shift;
#### do some check on $arg to see if it is a filehandle or a normal scalar
if ( ref($arg) eq 'GLOB' )
{ print "arg is a glob\n" }
else
{ print "arg is NOT a glob\n" }
}
Also if you have an open filehandle, is there a way to see what file it is
referencing?
A "filehandle" does not correspond to a "file". They are different things.
A filehandle corresponds to an input/output channel.
A filehandle might correspond to a file, but it might correspond
to any of several other things instead.
What file would you report for:
open FILE, 'echo "Hello World" |' or die ...
foo(\*FILE);
or
foo(\*STDOUT);
??
--
Tad McClellan SGML consulting
tadmc@xxxxxxxxxxxxxx Perl programming
Fort Worth, Texas
.
- References:
- Detecting filehandles
- From: Subotai
- Detecting filehandles
- Prev by Date: Re: Redirecting STDOUT to a file
- Next by Date: Re: reading web page content
- Previous by thread: Re: Detecting filehandles
- Next by thread: Can't locate "extract_file" via "Archive::Tar"
- Index(es):
Relevant Pages
|