Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Why is $. not zero-based?

by pg (Canon)
on Dec 29, 2002 at 04:21 UTC ( [id://222846]=note: print w/replies, xml ) Need Help??


in reply to Why is $. not zero-based?

Another thing worth to mention is that, Perl actually counts input line numbers separately for each file handler. $. is just an alias for the input line number of the last accessed handler.

In case, you can not determine which file handler is the last accessed, or such a determination is not easy to make, you have to use input_line_number against a particular file handler.

A piece of code to demo those mentioned:
test.pl: use IO::Handle; open(MYSELF1, "<", "test.pl"); open(MYSELF2, "<", "test.pl"); while (<MYSELF1>) { print "after reading from myself1: $.\n"; if ($. % 2) { <MYSELF2>; print "after reading from myself2: $.\n"; } print "returned values from input_line_number: ("; print MYSELF1->input_line_number(), ", "; print MYSELF2->input_line_number(), ")\n"; } close(MYSELF1); close(MYSELF2);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found