use strict; use warnings; use Data::Dump qw/pp dd/; $|=1; BEGIN { $\="\n"; my $old = $SIG{__WARN__}; $SIG{__WARN__} = sub { my ( $msg ) = @_ ; if ($msg =~ /Constant subroutine DOMAIN_PORT redefined/ ) { $SIG{__WARN__} = $old; print "REDEFINED", pp caller; package ALIEN; no warnings "redefine"; sub DOMAIN_PORT() { 666 } } } } sub ALIEN::DOMAIN_PORT() { "DUMMY" } package ALIEN; sub DOMAIN_PORT() { 53 } BEGIN { print "pre compile"; } sub test { print "Inside test: ", DOMAIN_PORT; } BEGIN { test(); print DOMAIN_PORT; } #### REDEFINED("main", "d:/tmp/pm/patch_constant.pl", 42) pre compile Inside test: 53 53