in reply to Perl's __LINE__ off by 2

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

Replies are listed 'Best First'.
Re^2: Perl's __LINE__ off by 2
by ysth (Canon) on Aug 26, 2025 at 04:05 UTC
    The contents will be much more clear if you $Data::Dumper::Useqq=1; before the say Dumper
        Not sure what you mean? Dumper doesn't AFAIK have interpretations of CR LF, it is just showing the data, either outputting it raw (for the CRLF-relevant characters) or as escapes with Useqq, where \r means chr(0x13) and \n means chr(0x10) (assuming non-EBCDIC). If perl is opening the source in text mode, a binmode *DATA; before the seek/read may be good (don't know if that even works or if *DATA is somehow magic in a way that prevents changing layers).
Re^2: Perl's __LINE__ off by 2 ($PERLDB)
by LanX (Saint) on Aug 26, 2025 at 16:10 UTC
    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