in reply to Re: Re: Passing filehandle to sub
in thread Passing filehandle to sub

is there a reson or particular situation where it's better to use a bare word?
Under the assumption that you're using 5.6+ I can't really think of any practical situations where a bareword filehandle is preferable to a lexical variable. Sure it's potentially less characters, by general practice visually distinctive in code and doesn't incur the overhead of a lexical variable (which, to be honest, is pretty much negligible in all but the most process intensive of operations) but I can't think of any practical situations where these factors outweigh the use of a lexical filehandle. I say practical situations because a bareword filehandle is great for obfuscation and golfing, but in code at large I'd say lexical filehandles would be prefered. If any monks can think of a situation where a bareword filehandle is indeed preferable to a lexical filehandle then please reply below.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Re: Re: Passing filehandle to sub
by Mr. Muskrat (Canon) on Jun 09, 2003 at 13:34 UTC

    Perhaps you are familiar with the saying: "You can't teach an old dog new tricks"?

    I (and I'm sure many others) learned to use bareword filehandles. If I remember to use a variable one time, chances are the next time I'll revert back to a bareword.

Re: Re: Re: Re: Passing filehandle to sub
by sauoq (Abbot) on Jun 09, 2003 at 21:41 UTC
    If any monks can think of a situation where a bareword filehandle is indeed preferable to a lexical filehandle then please reply below.

    When it is already open for you such as ARGV, ARGVOUT, and the STD* filehandles.

    Other than that, you got me... but I'm an old dog like Mr. Muskrat and tend to use them, at least in my main programs. (I manage to avoid them in modules though.)

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Re: Re: Re: Passing filehandle to sub
by bobn (Chaplain) on Jun 09, 2003 at 22:17 UTC
    One more reason TO use the variable method:

    Better error messages, as you can get the filename in messages using the following:

    my $f = "/etc/hosts"; open($f, $f); readit($f); sub readit { my $h = shift; print while(<$h>); warn; }
    The result:
    last line of output is:
    Warning: something's wrong at ./t25.pl line 11, </etc/hosts> line 12.


    --Bob Niederman, http://bob-n.com