in reply to Global variables in Perl
package ConfigThisJunk; use strict; use warnings; use Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( DEBUG ); use constant DEBUG => 1; 1;
use strict; use warnings; use ConfigThisJunk qw( DEBUG ); my $x = 123; print("debug: x is $x\n") if DEBUG;
Bonus: If DEBUG is false, the whole print-if is optimised away because it's a constant! (If it's true, the if part is optimised away.)
If you wanted the be able to change DEBUG, this is still cleaner:
package ConfigThisJunk; use strict; use warnings; use Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( DEBUG ); my $DEBUG = 1; sub DEBUG { if (@_) { $DEBUG = shift; } return $DEBUG; } 1;
use strict; use warnings; use ConfigThisJunk qw( DEBUG ); my $x = 123; print("debug: x is $x\n") if DEBUG;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Global variables in Perl
by taioba (Acolyte) on Jun 04, 2010 at 16:33 UTC | |
by ikegami (Patriarch) on Jun 04, 2010 at 17:43 UTC | |
by taioba (Acolyte) on Jun 04, 2010 at 20:11 UTC | |
by ikegami (Patriarch) on Jun 04, 2010 at 20:18 UTC | |
by taioba (Acolyte) on Jun 04, 2010 at 21:46 UTC | |
|