NAME
constant::our - Perl pragma to declare constants like our vars
VERSION
Version 0.05
SYNOPSIS
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"}'
DESCRIPTION
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: ..."
IMPORTANT
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.
EXPORT
Nothing by default.
SEE ALSO
constant::abs && constant::def
THANKS
Bolshakova Elena
Neil Bowers
AUTHOR
Green, <Evdokimov.Denis at gmail.com>
BUGS
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.
SUPPORT
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
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
COPYRIGHT & LICENSE
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.