in reply to Re: Re^4: Determining what line a filehandle is on
in thread Determining what line a filehandle is on

It's not terribly more difficult, but slightly. More wear on the fingers, use of modules, etc, but the result is crystal clear. Not the kind of thing you would do in a quick '-e', but if you want to communicate, this is the only way to fly:
use IO::Handle; open ($a, "ls|"); open ($b, "ls|"); while (<$a>) { print "A: ", $a->input_line_number(), " "; <$b>;<$b>; print "B: ", $b->input_line_number(), " "; } close ($a); close ($b);

Replies are listed 'Best First'.
Re (tilly) 7: Determining what line a filehandle is on
by tilly (Archbishop) on Jul 07, 2001 at 16:36 UTC
    Given that $a and $b are special to sort it is a bad idea to use them as regular variables.

    Also note that if you are willing to properly localize them with my, then you should not need to issue an explicit close. Of course declaring $a or $b to be lexical makes writing a sort subroutine hard (hence the first piece of advice).

    Finally I think your "only way to fly" comment is a bit strong...

      $foo and $bar would have done just as well, but after overexposure to Perl Golf, I'm tending to use shorter names anyway. $x and $y perhaps? Recently I was having trouble when I called a variable in a C++ structure "errno". Collisions occur in many languages.

      The "only way to fly" comment is about clarity of programming, not about the way to code. TMTOWTDI is always assumed, of course. IO::Handle's methods do seem to be the clearest from the point of maintenance and understanding, but I'm not advocating that you must use IO::Handle. I am advocating that people create code that works and makes sense.
Re: Re^6: Determining what line a filehandle is on
by John M. Dlugosz (Monsignor) on Jul 07, 2001 at 21:01 UTC
    On a related note, isn't
    $a= undef; open ($a, "<file.txt");
    a fairly new thing? That is, we used to have to use tricks to create a reference to an anonymous filehandle first, and then open it. Now filehandles are autovivafing. I think that may be one reason why getting away from plain HANDLE's is historicaly a pain, but isn't as bad anymore.

      I believe this was introduced in Perl 5, so your definition of "new" is obviously a matter of perspective.

      In fact, one of the things that I'm wondering is if you are supposed to be using "old-fashioned" file handles at all. They are more difficult to localize and pass as parameters. Using a "GLOB-ref", as these file handles are, is much more convenient, given even the one-character "penalty" for the $.

      Update:
      The "traditional" way is along the lines of:
      local (*FOO); open (FOO, $foo_file) || die "Could not open $foo_file\n"; DoStuffOnHandle(\*FOO); close (FOO);
      Versus the new "lexical" way:
      my $foo; open ($foo, $foo_file) || die "Could not open $foo_file\n"; DoStuffOnHandle($foo); close ($foo);
        Actually this was introduced in 5.6.0, so new really isn't that relative. In 5.005_03 and earlier you have to jump an extra hoop:
        my $foo = do {local *FH}; # etc as above
        or if you prefer a different look, you can use Symbol and then do it with:
        my $foo = gensym(); # And so on
        But with 5.6 it autovivifies for you, and that is rather nice.
        Well, that convinces me. Since there is no real issue, I'll use lexical variables going forward.