unconstant
view release on metacpan or search on metacpan
lib/unconstant.pm view on Meta::CPAN
package unconstant;
use Sub::Util ();
use warnings;
use constant ();
my $constant_import;
my $installed;
BEGIN { $constant_import = \&constant::import };
use 5.020;
use strict;
use warnings;
our $VERSION = '0.09';
our %declared;
#=======================================================================
# Some names are evil choices.
my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD UNITCHECK };
my %forced_into_main = map +($_, 1),
qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG };
my %forbidden = (%keywords, %forced_into_main);
my $normal_constant_name = qr/^_?[^\W_0-9]\w*\z/;
my $tolerable = qr/^[A-Za-z_]\w*\z/;
my $boolean = qr/^[01]?\z/;
sub unconstant_import {
return if $installed;
*constant::import = *constant_import;
$installed = 1;
}
sub unconstant_unimport {
return unless $installed;
no warnings 'redefine';
*constant::import = $constant_import;
$installed = 0;
}
sub constant_import {
my $caller = caller();
my $package = shift;
my $flush_mro;
return unless @_;
my $multiple = ref $_[0];
my $constants;
if ( $multiple ) {
if ($multiple ne 'HASH') {
require Carp;
Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'");
}
$constants = shift;
}
else {
unless (defined $_[0]) {
require Carp;
Carp::croak("Can't use undef as constant name");
}
$constants->{+shift} = undef;
}
my $symtab;
{
no strict 'refs';
$symtab = \%{$caller . "::"};
}
foreach my $name ( keys %$constants ) {
my $pkg = $caller;
my $symtab = $symtab;
my $orig_name = $name;
if ($name =~ s/(.*)(?:::|')(?=.)//s) {
( run in 4.302 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )