in reply to Line Numbers
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Line Numbers
by davido (Cardinal) on Oct 17, 2003 at 21:42 UTC | |
The preceeding code prints three sequential numbers. I bring this up because it could be guessed (or hoped) that __LINE__ might be relative to the top of the code being processed in the eval block. In fact, it is not, if it uses eval's {block} syntax. However, __LINE__ will return a different result if you wrap the eval'ed code in quotes. See the following code:
As you can see, that code prints the 6 and 7 as absolute file linenumbers. But the 1 and 3 are loosely relative to the quoted eval material. The "line 5" generated by trapping the die shows the odd behavior that in the case of lines of code eval'ed within quotes line numbers seem to skip a number for each line. Finally, the printing of '12' indicates that once back outside the eval function, the original numbering scheme resumes, unaffected by eval's apparent reset of __LINE__. So line must be localized within quoted eval strings. I'd be interested in seeing any documentation for this behavior.
Dave "If I had my life to do over again, I'd be a plumber." -- Albert Einstein | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Oct 17, 2003 at 22:37 UTC | |
I've fairly recently started coding my (rare) uses of eval like this.
This allows you to get very useful error messages from within the evals, telling you not only which line number within the eval failed and why, but also the package name and line number at which the eval is located. Not so important when the eval is in main as here, though knowing which line number is useful if there are more than one in the file, but it really comes into it's own when the eval is located in a module. Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham"Think for yourself!" - Abigail Hooray! | [reply] [d/l] [select] |
by liz (Monsignor) on Oct 18, 2003 at 20:46 UTC | |
Hmmm... that might be a nice pragmatic module: one that steals the standard eval() and prefixes any string eval with package and line number of caller:
Alas, it seems that the standard eval() cannot be replaced. ;-( Liz | [reply] [d/l] |
Re: Re: Line Numbers
by Anonymous Monk on Oct 17, 2003 at 21:33 UTC | |
| [reply] |
Re: Re: Line Numbers
by BazB (Priest) on Oct 17, 2003 at 21:27 UTC | |
Update: Correction; after re-reading perlvar myself, it states "$0...Contains the name of the file containing the Perl script being executed". Update 2: I am an idiot. I've double checked, following vek's post (++); he's quite right. $0 does exactly as advertised. If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong. | [reply] [d/l] [select] |
by vek (Prior) on Oct 18, 2003 at 20:29 UTC | |
Actually on Solaris 8 it's just /path/to/perl/script.
Prints /path/to/perl/script on Solaris 6, 7, 8, 9 - I just ssh'd into work to test it out :-)
--
vek
--
| [reply] [d/l] [select] |