in reply to Passing filehandles to subroutines

I never use global file-handle identifiers any more. Without pulling in the whole File::IO module, you can still do this:

open (my $in, $infile) or die; foo ($in); sub foo { my $file= shift; print $file "Some Stuff."; # note no comma }
Basically, a reference to a glob can be used just like a glob to mean the associated file handle. In Perl 5.6 I think the open function will auto-vivify to just that, so you don't need to use a module or tricks to create a reference to an anonomous glob anymore. There's been discussions on that here; I posted a question concerning using this as the prefered method moving forward, when I heard about the auto-vivification.

—John

Replies are listed 'Best First'.
Re: Re: Passing filehandles to subroutines
by ibanix (Hermit) on Jan 27, 2003 at 07:11 UTC
    John,

    This is very cool. I like this method. Do you have any idea if this is going to be supported in Perl 6? Or, perhaps I should ask, is this 'kosher'? This isn't a "may be removed if we feel like it" feature, is it?

    Thanks!

    ibanix

    $ echo '$0 & $0 &' > foo; chmod a+x foo; foo;
      See if you can find my thread via super search or skimming the list of nodes I posted. That is when I asked if it was 'kosher' and got a lot of remarks about it. What I remember: yes, it is proper and will work moving forward, but doesn't work on older versions (I don't remember when) so it is an issue if you need to run on older stuff, but no other issues impressed me enough to remember or disuade me.

      —John

        I think it's supported since version 5.6.

        Arjen

      It's pretty normal as far as I know, I use it all the time. Look at the perlsub documentation about passing around filehandles.

      C.