Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re^4: Determining what line a filehandle is on

by John M. Dlugosz (Monsignor)
on Jul 07, 2001 at 05:08 UTC ( [id://94659]=note: print w/replies, xml ) Need Help??


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

I agree with the reply that the magical variables all have a recognisable form, so that a programmer might not know what $. or $] means, but knows it is one of those magical variables and can look it up.

However, calling $input->input_line_number() is quite clear, and according to the docs, is a true method that works on the object called with, rather than doing something counterintuitive like input_record_separator() which is actually global.

What do you mean by "makes things more difficult to implement"? You mean in general, or because this simple task is so small it's not with the overhead?

—John

Replies are listed 'Best First'.
Re^6: Determining what line a filehandle is on
by tadman (Prior) on Jul 07, 2001 at 12:04 UTC
    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);
      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.
      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);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://94659]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found