Alternatively, if you don't like the ref solutions, you can use one of perl's Obscure Open Tricks

sub your_function { my ( $string, $in_fh, $out ) = @_; local *OUTFH; # opens $out if it's a filename # dupes $out if it's a filehandle open OUTFH, ">$out" or die "$out: $!\n"; # do whatever with $in_fh and $string while( <$in_fh> ) { /$string/o and print OUTFH "$string: $_" } # close OUTFH: either we opened it or we duped it # Either way we don't need it any more. close OUTFH; }

Then your_function() just relies on its caller to do the right thing. If you want to pass your_function() a filename to take as output, then you'd call it as you'd expect:

your_function( $string, \*INPUT, '/tmp/somefile' );

On the other hand, if you want to pass your_function() an already opened filehandle, and use that for output, then you'd call it as:

# print to STDOUT instead of to disk your_function( $string, \*INPUT, "&STDOUT" ); # print to already-opened filehandle open OUTPUT, '> /tmp/somefile' or die "/tmp/somefile: $!\n"; your_function( $string, \*INPUT, "&OUTPUT" );

For more info on this, check out Obscure Open Tricks in perlopentut. There's a recommendation in there that you qualify the package name where OUTPUT is located, which I've ommitted in the code above.

One thing worth noting is that the line

open OUTFH, ">$out" or die "$out: $!\n";

Must appear as written (i.e. with no spaces between the > and $out). Avoid these following variations, since they don't do the same thing as the line above

# wrong: given, eg, &STDOUT, creates a file named '&STDOUT' open OUTFH, "> $out" or die "$out: $!\n"; # wrong: also creates, eg, '&STDOUT' open OUTFH, '>', $out or die "$out: $!\n";

In reply to Re: Accepting either a filename or a handle by belden
in thread Accepting either a filename or a handle by fizbin

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.