#!/usr/bin/perl use warnings; use strict; use P; use parent 'Switches'; ## example of a PERL compile-time constant ## based on a command line argument "-NODEBUG" ## using 'Switches' & BEGIN ## to run, place "-DNODEBUG" on the command line. (law) BEGIN { Switches->process_options({DNODEBUG => { act => [ sub { eval '# line '.__LINE__.' "'.__FILE__.'" use constant "_NODEBUG_"=>1;'; $@ && P::Pe("eval: %s\n", $@); } ], } }, \@ARGV); eval 'use constant _NODEBUG_ => undef;' unless *main::_NODEBUG_{CODE}; } sub declared ($) { use constant 1.01; # note: needed for "declared", below my ($name, $pkg) = (shift, caller); # prepend module name (must be a full-name) $name = q(main) . $name if '::' eq substr $name,0,2; # use caller pkg if needed my $full_name = 0 <= index($name,'::') ? $name : $pkg.'::'.$name; $constant::declared{$full_name}; } P "_NODEBUG_ is a %s %s", declared(q(_NODEBUG_)) ? "constant" : "sub", P "and is %s", _NODEBUG_ ? "TRUE (devel checks disabled)." : "FALSE (devel checks enabled)."