Why $NR and $INPUT_LINE_NUMBER are not the same as $.?

P

Peng Yu

I have the following perl script.

$ cat main.pl
#!/usr/bin/env perl

#use warnings;

while(<STDIN>) {
print "#$_";
print "\$INPUT_LINE_NUMBER=$INPUT_LINE_NUMBER\n";
print "\$NR=$NR\n";
print "\$.=$.\n";
}

However, the output shows that $NR and $INPUT_LINE_NUMBER are not the
same as $.. According to my understand, these three variable should be
the same. Would you please let me know why they are not the same in
this case? And how to make them the same?

$ ./main.pl < main.pl
##!/usr/bin/env perl
$INPUT_LINE_NUMBER=
$NR=
$.=1
#
$INPUT_LINE_NUMBER=
$NR=
$.=2
##use warnings;
$INPUT_LINE_NUMBER=
$NR=
$.=3
#
$INPUT_LINE_NUMBER=
$NR=
$.=4
#while(<STDIN>) {
$INPUT_LINE_NUMBER=
$NR=
$.=5
# print "#$_";
$INPUT_LINE_NUMBER=
$NR=
$.=6
# print "\$INPUT_LINE_NUMBER=$INPUT_LINE_NUMBER\n";
$INPUT_LINE_NUMBER=
$NR=
$.=7
# print "\$NR=$NR\n";
$INPUT_LINE_NUMBER=
$NR=
$.=8
# print "\$.=$.\n";
$INPUT_LINE_NUMBER=
$NR=
$.=9
#}
$INPUT_LINE_NUMBER=
$NR=
$.=10
#
$INPUT_LINE_NUMBER=
$NR=
$.=11
 
U

Uri Guttman

PY> However, the output shows that $NR and $INPUT_LINE_NUMBER are not the
PY> same as $.. According to my understand, these three variable should be
PY> the same. Would you please let me know why they are not the same in
PY> this case? And how to make them the same?

you need to use English to enable the spelled out variables. this is
documented at the very beginning of perlvar. always read that when you
first have a question on any of perl's builtin variables.

uri
 
P

Peng Yu

  PY> However, the output shows that $NR and $INPUT_LINE_NUMBER are notthe
  PY> same as $.. According to my understand, these three variable should be
  PY> the same. Would you please let me know why they are not the same in
  PY> this case? And how to make them the same?

you need to use English to enable the spelled out variables. this is
documented at the very beginning of perlvar. always read that when you
first have a question on any of perl's builtin variables.
Sorry, I directly jumped to the description of $., and incorrectly
assume that the three variables are the same by default.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top