Search code examples
perlconstantfolding

Is __LINE__ constant-folded in this Perl one-liner?


In exploring an alternative answer to sarathi's current file line number question, I wrote this one-liner with the expectation that it would print the first line of all files provided:

$ perl -ne 'print "$ARGV : $_" if __LINE__ == 1;' *txt

This did not work as expected; all lines were printed.

Running the one-liner through -MO=Deparse shows that the conditional is not present. I assume this is because it has been constant-folded at compile time:

$  perl -MO=Deparse -ne 'print "$ARGV : $_" if __LINE__ == 1;' *txt
LINE: while (defined($_ = <ARGV>)) {
    print "$ARGV : $_";
}
-e syntax OK

But why?

Run under Perl 5.8.8.


Solution

  • __LINE__ corresponds to the line number in the Perl source, not in the input file.