constant::our - Perl pragma to declare constants like our vars
Version 0.07
use constant::our { DEBUG => 1 }; use constant::our { DEBUG_SQL => 1, DEBUG_CACHE => 1, }; ###################### package My::Cool::Tools; use constant::our qw(DEBUG DEBUG_SQL); if(DEBUG) { warn "DEBUG: $debug_info"; if(DEBUG_SQL) { warn "DEBUG_SQL: $querty"; } } # or DEBUG && warn "DEBUG: $debug_info"; DEBUG && DEBUG_SQL && warn "DEBUG_SQL: $querty"; # Environment $ export CONSTANT_OUR_DEBUG=1 $ perl -e'use constant::our qw(DEBUG); DEBUG && {warn "Running in debug mode"}'
This pragma extends standard pragma 'constant'.
As you may know, when a constant is used in an expression, Perl replaces it with its value at compile time, and may then optimize the expression further.
You can inspect this behavior by yourself:
$ perl -MO=Deparse -e'use constant{DEBUG => 1}; warn "1"; if(DEBUG){warn "2"} warn "3";' use constant ({'DEBUG', 1}); warn '1'; do { warn '2' }; warn '3';
All warns are here.
$ perl -MO=Deparse -e'use constant{DEBUG => 0}; warn "1"; if(DEBUG){warn "2"} warn "3";' use constant ({'DEBUG', 0}); warn '1'; '???'; warn '3';
Notice the '???' instead of the second 'warn'.
So you can do something like this:
# in the main script use constant DEBUG => 0; # in a module if(main::DEBUG) { # some debug code goes here }
But you should declare all constants you use, you can't simply write
if (main::DEBUG_SQL) { }
without corresponding
use constant DEBUG_SQL => 0;
in the main script.
With constant::our you can freely use "undeclared" constants in your condition statements.
# main script use constant::our { DEBUG => 1, DEBUG_CACHE => 1, }; ###################### package My::Cool::Tools; use constant::our qw(DEBUG DEBUG_SQL); # don't need DEBUG_CACHE, but want (undeclared) DEBUG_SQL DEBUG && warn "DEBUG: $debug_info"; # DEBUG --> 1 DEBUG && DEBUG_SQL && warn "DEBUG_SQL: $query"; # DEBUG_SQL --> undef stderr: "DEBUG: ..."
$ export CONSTANT_OUR_DEBUG=1 $ perl -e'use constant::our qw(DEBUG); DEBUG && {warn "Running in debug mode"}'
use constant::our {CONST => 123}; use ... use ... use Data::Dumper; $Data::Dumper::Sortkeys = 1; print Dumper \%constant::our::package_use; print Dumper \%constant::our::package_set; print Dumper \%constant::our::package_set_implicitly;
A constant should be declared no more than one time. If you try to declare a constant twice (with different values), your program will die.
Since use of undeclared constant implicitly declares it, you should declare your constants _before_ you start use them.
Nothing by default.
constant
constant::abs && constant::def
Bolshakova Elena
Neil Bowers
Green, <Evdokimov.Denis at gmail.com>
<Evdokimov.Denis at gmail.com>
Please report any bugs or feature requests to bug-constant-our at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=constant::our. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-constant-our at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc constant::our
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=constant::our
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/constant::our
CPAN Ratings
http://cpanratings.perl.org/d/constant::our
Search CPAN
http://search.cpan.org/dist/constant::our
Copyright 2009 Green, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install constant::our, copy and paste the appropriate command in to your terminal.
cpanm
cpanm constant::our
CPAN shell
perl -MCPAN -e shell install constant::our
For more information on module installation, please visit the detailed CPAN module installation guide.