in reply to Passing filehandle to sub

If you've opened it with a bareword then pass the glob, or if you've used a variable just pass that e.g
## bareword open open(FH, "yourfile") or die("oops - $!"); func_call( *FH ); ## variable open open(my $fh, "yourfile") or die("oops - $!"); func_call( $fh ); sub func_call { my $fh = shift; do_stuff($_) while <$fh>; }
And if you need to revert back to previous positions in the file make use of the tell and seek functions.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: Passing filehandle to sub
by arthas (Hermit) on Jun 09, 2003 at 12:27 UTC

    Hi broquaint!

    I've got a question: since it seems that opening a file using a variable (instead of a bare word) only has advantages, like in this case, is there a reson or particular situation where it's better to use a bare word?

    Thanks, Michele.

      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

        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

        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.

        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: Passing filehandle to sub
by marctwo (Acolyte) on Jun 09, 2003 at 13:19 UTC
    I used the variable method you suggested and it seems to work fine.

    Thanks for your quick and helpful response!

    Marc