use 5.012; use Sub::Name 'subname'; use B; sub get_sub_name { B::svref_2object(shift)->GV->NAME } my $ref = sub { say 'Our name is: ' . (caller(0))[3] }; $ref->(); say 'Before subname: ' . get_sub_name($ref); subname 'the_pad_test', $ref; $ref->(); say 'After subname: ' . get_sub_name($ref); print "the_pad_test(): "; eval { the_pad_test() }; print $@ =~ /^Undefined sub/ ? "fails (correct)\n" : "ERROR: $@"; #### Our name is: main::__ANON__ Before subname: __ANON__ Our name is: main::the_pad_test After subname: the_pad_test the_pad_test(): fails (correct)