package Bark; use strict; use warnings; # more code here foreach (qw(cat dog)) { my $method_name = "function_$_"; # We're going to close over this variable # so it must be fresh in every loop iteration # don't use loop counter here my $impl = $_; # pre-caclucate more variables here if needed. # do whatever normal "sub $method {...}" would do # while we're still in strict mode my $code = sub { print $impl; }; # Finally, plant method - stricture is ONLY turned off # until end of scope i.e. for 1 line only no strict 'refs'; ## no critic # just in use "perlcritic" *$method_name = $code; }; 1;