If both constants are false, the print and the if are removed at compile-time.
If one or both constants are true, the if is removed at compile-time, leaving the print to execute unconditionally.
By the way, the benefits of this are minuscule. You might save yourself a couple of seconds every 100,000 times you execute it. The time it takes to evaluate the conditional expression is dwarfed by the time it takes to call the function.
Update:
Rate var const var 765464/s -- -20% const 956589/s 25% --
use Benchmark qw( cmpthese ); { package pkg1; use constant DEBUG => 0; our $DEBUG => 0; } { package pkg2; use constant DEBUG => 0; our $DEBUG => 0; } sub const { print "boo" if pkg1::DEBUG || pkg2::DEBUG; } sub var { print "boo" if $pkg1::DEBUG || $pkg2::DEBUG; } cmpthese(-3, { const => 'const(); 1;', var => 'var(); 1;', });
In reply to Re: use constant and the Perl Optimizer
by ikegami
in thread use constant and the Perl Optimizer
by Outaspace
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |