eval 'BEGIN { $^C = 1 } ' . $code; # where $code contains what you want to test #### use Safe; my $compartment = new Safe; $compartment->reval( 'no warnings; BEGIN { $^C = 1 } ' . $code );