in reply to Re: Passing a Filehandle that Might be a Bareword
in thread Passing a Filehandle that Might be a Bareword
I would encourage passing in via \*STDOUT over *STDOUT. Globs are a type from Perl 4 and look too much like strings. Checking !ref($arg) is much more common than checking "GLOB" eq ref(\$arg) and !ref($arg) needs to be checked anyway in order to deal with lexical file handles and \*STDOUT and *STDOUT{IO}.
Supporting passing in "STDOUT" gets rather tricky. Consider:
package Whatever; use My::Logger qw< setLogLevel >; setLogLevel( STDOUT => 1 ); open LOG, '>>', 'some.log' or die; setLogLevel( LOG => 2 );
My::Logger::setLogLevel() needs to treat 'STDOUT' as *STDOUT (or *main::STDOUT) but must treat 'LOG' as *Whatever::LOG.
my( $arg )= @_; my $fh; if( ref( $arg ) ) { $fh= $arg; } elsif( "GLOB" eq $arg ) { $fh= \$arg; # or $fh= *{$arg}{IO}; } else { # It is a plain string. If you want to # treat it as the name of a glob, then # you'll need to check for /::|'/ and # prepend caller().'::', unless it is # special like 'STDOUT'. # You'll also need 'no strict;' here. ... }
- tye
|
|---|