Perl Supports #line

I didn't see this anywhere in the Perl documentation, but it's a useful piece of information to know, if you ever find yourself generating Perl code. Perl supports C preprocessor-style #line directives. For example:

    #line 135 script.ci
    die;

The above script will print "Died at script.ci line 135". If you have a syntax error, then the Perl interpreter will claim that the error is at "script.ci line 135". And so on. This is extremely useful for embedding perl snippets inside other files, because you can cause the Perl interpreter to report all errors as if they were in the original file.

What I like most about this bit of hackery is that # is the comment character for Perl, so normally the entire line directive would have been ignored as a comment -- obviously Perl has a special-case hack for supporting this. I think that Perl's handling of #line directives reveals a tiny window through which you can see the entire design philosophy of Perl: in any battle of consistency versus usefulness, usefulness wins.

Posted on September 24, 2003 01:47 PM
More languages articles

Comments
Post a comment









Remember info?




Prove you're human. Type "human":