It's occasionally useful to have a block of Perl code that you use only for debugging or development tweaking. That's fine, but having blocks like this can be expensive to performance, particularly if the decision whether to execute it is made at runtime.

Curtis "Ovid" Poe recently wrote a module that can help with this problem: Keyword::DEVELOPMENT. The module utilizes Keyword::Simple and the pluggable keyword architecture introduced in Perl 5.012 to create a new keyword: DEVELOPMENT. It uses the value of the PERL_KEYWORD_DEVELOPMENT environment variable to determine whether or not a block of code is to be executed.

Using it couldn't be easier:

use Keyword :: DEVELOPMENT ;



sub doing_my_big_loop {

my $self = shift ;

DEVELOPMENT {

# insert expensive debugging code here!

}

} Keyworddoing_my_big_loopDEVELOPMENT

At compile time, the code inside the DEVELOPMENT block is optimized away and simply doesn't exist.

Do you see the advantage here? Set up the PERL_KEYWORD_DEVELOPMENT environment variable to be true on your sandbox and false on your production environment, and valuable debugging tools can be committed to your code repo, always there when you need them.

You could also use this module, in the absence of a more evolved configuration management system, to handle variations in settings between production and development or test environments:

sub connect_to_my_database {



my $dsn = "dbi:mysql:productiondb" ;

my $user = "db_user" ;

my $pass = "db_pass" ;



DEVELOPMENT {

# Override some of that config information

$dsn = "dbi:mysql:developmentdb" ;

}



my $db_handle = DBI -> connect ( $dsn , $user , $pass ) ;

} connect_to_my_databaseDEVELOPMENTDBI

Later enhancement to this snippet would have you reading in configuration information from somewhere else, perhaps from a YAML or INI file, but I hope you see the utility here.

I looked at the source code for Keyword::DEVELOPMENT and spent about a half hour wondering, "Gosh, why didn't I think of that?" Once Keyword::Simple is installed, the module that Curtis has given us is surprisingly simple. It's an elegant solution to something I've needed in my own coding practice for a long time.