TorontoJim has asked for the wisdom of the Perl Monks concerning the following question:

I have a script doing some counting and logging for debugging/development. I have this line to record the file name and physical line of the statement:

$spinner->count('benchLoop', __FILE__, __LINE__);

While I have this on line 93 of the physical file, Perl __LINE__ is reporting line 95. THere is nothing else on that line, there is no modification of the value in the sub handling it. __FILE__ is reported correctly.

Any idea why __LINE__ would be consistently off by 2?

Replies are listed 'Best First'.
Re: Perl's __LINE__ off by 2
by Corion (Patriarch) on Aug 25, 2025 at 10:33 UTC

    If you're using Filter::Simple (or any other kind of source filter), that can easily throw __LINE__ off from what you see in your editor (example).

    Another situation where the line numbers deviate from the source code is when you have multiline if statements, but here Perl reports (for example in warnings) the location of the if, not the location where the reason for a warning was encountered.

    I'm not aware of other situations where __LINE__ differs from what you see in your text editor, but maybe with creative use of here-docs, you can provoke them.

Re: Perl's __LINE__ off by 2
by choroba (Cardinal) on Aug 25, 2025 at 10:17 UTC
    A comment of the form
    # line 120
    would set the following line's number to 120. Does it happen in the script?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Perl's __LINE__ off by 2
by TorontoJim (Beadle) on Aug 25, 2025 at 09:37 UTC
    I created a new file and tested this. This time __LINE__ is reporting correctly. I think I may have a mix of line feed styles that are causing a difficulty with how Perl counts the lines. It's the only thing I can think of. Any other thoughts would be welcome.
Re: Perl's __LINE__ off by 2 (Introspection)
by LanX (Saint) on Aug 25, 2025 at 22:53 UTC
    First of all, this

    > $spinner->count('benchLoop', __FILE__, __LINE__);

    can be reduced to $spinner->count('benchLoop') if you check caller to retrieve file and line inside the count() method.

    Choroba's tip to use # line 123 directive is legit, see Plain-Old-Comments

    So try to debug where the "strangeness" starts to happen in your file.

    :~$ perl $\="\n"; print __LINE__; # line 120 print __LINE__; __END__ 2 120

    > Any idea why __LINE__ would be consistently off by 2?

    Like Corion said, any module injecting code, like by using source filters could silently add additional lines.

    Regarding your newline theory, try to end your file with __DATA__ and put this into your code right before calling ->count to check what the engine actually sees as different lines before compiling.

    seek DATA,0,0; my @lines = <DATA>; say Dumper \@lines;

    for instance

    use v5.12; use warnings; use Data::Dumper; say __LINE__; say __LINE__; say __LINE__; seek DATA,0,0; my @lines = <DATA>; say Dumper \@lines; __DATA__

    output
    4 5 6 $VAR1 = [ 'use v5.12; ', 'use warnings; ', 'use Data::Dumper; ', 'say __LINE__; ', 'say __LINE__; ', 'say __LINE__; ', ' ', ' ', 'seek DATA,0,0; ', 'my @lines = <DATA>; ', 'say Dumper \\@lines; ', ' ', '__DATA__ ' ];

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      The contents will be much more clear if you $Data::Dumper::Useqq=1; before the say Dumper
      here yet another approach to automatically introspect your source by using debugger flags - see $PERLDB:

      (Crossposted at Reddit while PM was down)

      BEGIN { $^P |= 0x400;}; use v5.12; use warnings; use Data::Dump; say "some code"; *DB::dbline = $::{ "_<" . __FILE__ }; ddx @DB::dbline;

      OUTPUT:
      perl /home/lanx/perl/pm/source_introspection.pl some code # source_introspection.pl:9: ( # undef, # undef, # "use v5.12; \n", # "use warnings;\n", # "use Data::Dump;\n", # "\n", # "say \"some code\";\n", # "\n", # "*DB::dbline = \$::{ \"_<\" . __FILE__ };\n", # "ddx \@DB::dbline;\n", # )

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery