Using the import facility:
package MyClass;
my $class_variable;
sub import
{
(undef, my $new_class_variable) = @_;
if (defined $class_variable and
defined $new_class_variable and
$class_variable ne $new_class_variable)
{
warn '$MyClass::class_variable redefined';
}
$class_variable = $new_class_variable if defined $new_class_variable;
}
Pass the value when you use the module:
use MyClass qw(42);
It's not exactly idiomatic Perl, but it's not uncommon either. That sanity check in the middle of the function should give you a hint about why it might not be the best approach in all cases. If MyClass is only supposed to be use
d from a top-level script, you could enforce that sanity check instead:
caller eq 'main' or die 'MyClass can only be used from package main';