Constant Dangers
Perl Best Practices (p. 56-57) warns against use constant, and advises using the Readonly module if possible. use constant constants cannot be interpolated, nor can they be created at run time:
use constant ( PI => 3 ); print 'PI is ' . PI . "\n"; use Readonly; Readonly my $PI => atan2( 0, -1 ); print "PI is $PI\n";
Additional dangers of use constant involve open calls that mistakenly pick a file handle name that conflicts with a constant:
#!/usr/bin/perl -w use strict; use constant ( PASSWD_FILE => '/etc/passwd' ); my $tmp_passwd_filename = shift || die "Usage: $0 filename\n"; # … and much later, a nasty bug results open PASSWD_FILE, '<', $tmp_passwd_filename or die "error: could not open: file=" . $tmp_passwd_filename . "\n";
Instead, always use variables to hold file handles, and avoid the risk of a constant name conflicting with a file handle. This method also avoids conflicts between subroutines imported from other modules and constants:
open my $passwd_fh, '<', $tmp_passwd_filename;