in reply to Re^3: Macro in perl code?
in thread Macro in perl code?
?!... sub X() { 0 } print "$message1 \n" if( runtime cond); ... sub X() { 0 } print "$message2 \n" if( runtime cond); ...
I'm confused. Would be nice if you can shed a bit more light on it - thanks!... print "$message1 \n" if( runtime cond); ... print "$message2 \n" if( runtime cond); ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Macro in perl code?
by shmem (Chancellor) on Oct 17, 2007 at 08:53 UTC | |
Erm, no. The sub and the print statement being on the same line is an artifact of the code being a oneliner :-) The point is that
is folded into a constant at compile time (more accurate after compiling - while optimizing), and the resulting constant is used by the optimizer to further reduce the optree throwing away code which, depending on that constant, is never reached. So, it's not a (runtime cond). You get a mandatory warning if you redefine the constant defining sub X() { } at runtime, and you won't get back code that has been optimized away after compile. I assumed that since the OP did preprocessing with m4 even before the perl compiler sees the code, the value for his $verbose doesn't change at runtime, but I may be mistaken. Conditional code that depends (or could depend) on runtime state can't be optimized that way, so there's a difference in writing
and
Only in the latter case the if() block is optimized away, even if, in the former, there's no place in the scope in which the my $debug is declared, where its value would change.
--shmem
| [reply] [d/l] [select] |
by Krambambuli (Curate) on Oct 17, 2007 at 09:21 UTC | |
| [reply] |
by pileofrogs (Priest) on Oct 17, 2007 at 16:01 UTC | |
Ah.. no wonder I couldn't make it work... I am using a runtime condition. The idea was rather than call a function which evaluates the cond and may simply exit, put the code inline and avoid the function call overhead. | [reply] |
by shmem (Chancellor) on Oct 17, 2007 at 19:44 UTC | |
But! since you wrote My script has the global $verbosity which is set by command line args. I ask - does this value change ever after? Because if not, you can make that a static via
of course only after examining under severe scrutiny what $verbosity holds (untaint, perlsec). Since the BEGIN block is a separate compile & run, the sub is defined and a constant sub after that, and its value can be used for optimizations. If the values you compare $verbose with are plain numbers as the 1 in
then optimization should work, because then you have constant vs. constant comparisons. Let's see...
bingo :-) --shmem
| [reply] [d/l] [select] |
by ikegami (Patriarch) on Oct 17, 2007 at 19:53 UTC | |
It sounds like you have a limited number of conditions (the different levels of verbosity), so you could something like the following:
Update: Ah woops! There'll be a prototype mismatch. The best you can do (without playing with the opcode tree) is:
Tested
I'm betting this is the fastest you'll get. | [reply] [d/l] [select] |