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        


In reply to Re^2: Passing a Filehandle that Might be a Bareword (\*) by tye
in thread Passing a Filehandle that Might be a Bareword by Xiong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.