Re: Detecting filehandles



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
.



Relevant Pages

  • Re: Detecting filehandles
    ... $arg = shift; ... do some check on $arg to see if it is a filehandle or a normal scalar ...
    (comp.lang.perl.misc)
  • Re: Too many open files on OpenBSD
    ... i suppose the src of the parse function helps (probably disobeying all ... for line in fileHandle do ... nil # comment ... @rootNode.addRunFile arg ...
    (comp.lang.ruby)
  • Re: Detecting filehandles
    ... 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. ... $ perl -le' ...
    (comp.lang.perl.misc)
  • Re: Detecting filehandles
    ... $arg = shift; ... do some check on $arg to see if it is a filehandle or a normal ...
    (comp.lang.perl.misc)
  • Re: Encrypted passwords
    ... using pw's instrumentation for passing a password ... to it via filehandle would simplify things a bit, ...
    (FreeBSD-Security)