tags:

views:

118

answers:

3

Line directives (#line) are used to reconfigure perl's idea of the current filename and line number. When is this required to get right filename and line number (in an error message)?

+5  A: 

They're useful when wrapping a Perl script in another file, like pl2bat does. Perl doesn't see the batch commands at the beginning of the file which throws off its idea of line numbers. A #line directive at the beginning of the Perl source compensates for this.

Michael Carman
+10  A: 

Usually such markers are put into code that has been pre-processed or mechanically generated in order to refer back to the human-generated source.

For example, if there was a program that converted Python to Perl, it might insert a

# line 812 "foo.py"

so that error messages would refer to the original Python code which would make more sense to the programmer.

msw
+1  A: 

In addition to the already mentioned reasons perl has a (strongly discouraged) -P option that runs the Perl file through a C preprocessor before it is executed. Since most C preprocessor's will use line directives when they include or remove part of a file so any errors will be reported from where they were located in the original source instead of the processed source.

Line directives can also be very useful if you are generating code in strings that is then passed to eval. Normally if there is a warning or error in such code you get an error reported like "died at (eval 1) line 1." Using line directives you can supply a useful file name and line number.

Ven'Tatsu