in reply to Sending filehandles?

Substituting a global file handle with a lexical has already been shown. But you can also pass around a typeglob. The syntax for that is:
use strict; package Foo; sub line { my $fh = shift; return <$fh>; } package main; open FH, $file; # depending on the perl version, use 3 argument open my $line = Foo::line(*FH);

update: that said, passing around a file handle is icky. You should reconsider your layout and encapsulate file operations into functions or methods. Make those callable from elsewhere, probably via an object.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Sending filehandles?
by chromatic (Archbishop) on Feb 08, 2008 at 21:20 UTC
    But you can also pass around a typeglob.

    Sure, and you can also get the underlying file descriptor from the filehandle and pass that around and dup a new filehandle to the file descriptor, but don't do that.

    Seriously. Why?

      Well, er... it's... because you can? Not sure - TIMTOWTDI? Well, not all what can be done should be done. Updating node...

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re^2: Sending filehandles? (\*)
by tye (Sage) on Feb 09, 2008 at 08:19 UTC

    Better to pass a ref to the glob, \*FH, than *FH. 1) The file handles created by "open my ..." and by things like IO::Handle->new( ... ) are all references to globs so everybody will be getting something that they expect to get. 2) It is a pain to tell *FH from "*FH" and so code that is only slightly naive might not realize that you passed in a file handle (such as code that expects either a handle or a file name).

    And, yes, chromatic, I've certainly done that. For one, the "open my ..." trick isn't backward compatible (and doesn't even fail in a way that makes one likely to think "oh, that doesn't work on this version of Perl") and the alternatives that are backward compatible to the oldest deployed Perls in environments I've recently worked in are a bit obnoxious. For another, there are very few problems with using open FOO, ... in relatively small code, thanks in large part to package.

    - tye