# file: constant.pm package constant; use warnings; use strict; use File::Spec; my %Overrides; sub make_inc_hook { # Make sure we don't load this file... or we'll recurse infinitely later. my ($path) = ( grep { -f $_ } map { File::Spec->catfile( $_, 'constant.pm' ) } grep { $_ ne q{.} } # avoids our directory @INC ) or die q{couldn't load constant.pm}; open my $fh, '<', $path or die "open: $!"; # Filter the real constant.pm, rename package to old_constant. my $source_filter = sub { return 0 unless defined $_; # edit: added 'defined' s/package constant;/package old_constant;/; return 1; }; # Returns an @INC hook. See perldoc -f require. return sub { my ($self, $file) = @_; return unless $file eq 'old_constant.pm'; return ($fh, $source_filter); }; } BEGIN { push @INC, make_inc_hook(); } # Preload/compile the original constant.pm use old_constant; sub import { my ($pkg) = caller; if ( ref $_[1] eq 'HASH' ) { my $param = $_[1]; $Overrides{ $_ } = $param->{ $_ } foreach keys %{ $param }; return; } my (undef, $name, $value) = @_; $value = $Overrides{ $name } if $Overrides{ $name }; use Data::Dumper; local $Data::Dumper::Terse = 1; eval qq{package $pkg; use old_constant '$name' => } . Dumper( $value ); die if $@; return; } 1;