in reply to Passing a file handle to a sub. I still don't get it.

How do you know what $_ really is in your program?

What you saw is the stringification, which is not necessarily the same thing. For instance:

my $x = \*FH; print "plain glob ref: <$x>\n"; # prints: plain glob ref: <GLOB(0x1d6c660)> open ($x, "<","somefile") or warn "error\n"; print "open FH ref: <$x>\n"; # prints: open FH ref: <GLOB(0x1d6c660)>
But if instead I use x to peek at the globref in the debugger, it looks like this:
my $x = \*FH; x $x # prints: #0 GLOB(0x1d6c408) # -> *main::FH open ($x, "<","somefile") or warn "error\n"; print "open FH ref: <$x>\n"; # prints: 0 GLOB(0x1d6c408) -> *main::FH FileHandle({*main::FH}) => fileno(6)
...which is much more interesting.

What do you get in your code?

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^2: Passing a file handle to a sub. I still don't get it.
by rvosa (Curate) on Jun 09, 2005 at 19:21 UTC
    How do you know what $_ really is in your program?

    I can see that get printed on milk cartons one day :) (Talk to your children! A message from the government of Canada.)

    Anyway, all kidding aside, here's what I'm doing, and now It Just Works, so thank you all for your thoughts and input. The calling sub:
    #... blah blah blah open(FH, $opts{-file}); $opts{-handle} = *FH; return $parser->from_handle(%opts); #... la dee da
    And the callee:
    #... stick out left leg while ( readline($opts{-handle}) ) { if ( $_ ) { # ... frobnicate } } # return right leg
      It may work for you now, but do you know why? You've fixed it, but can you duplicate it in other contexts?

      I notice that you no longer take a reference to a glob, but the glob directly. My quick check shows that this works (though the glob reference shows more info in the debugger with the x command).

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of