Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of perl's #line directives?

Tags:

debugging

perl

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)?

like image 856
Eugene Yarmash Avatar asked Mar 10 '10 16:03

Eugene Yarmash


2 Answers

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.

like image 195
msw Avatar answered Oct 09 '22 06:10

msw


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.

like image 33
Michael Carman Avatar answered Oct 09 '22 07:10

Michael Carman