#!/usr/bin/perl use warnings; use strict; use P; { package pkg; use warnings; use strict; use P; use Types::Core; our $cnt=0; sub new { my $p = shift; my $c = ref $p || $p; Pe "pkg %s created", ++$cnt; $p = bless { cnt => $cnt, fnc => sub { my $p2 = ref $_[0] ? shift : __PACKAGE__; Pe "fnc %d calling %s", $cnt, $p2->FUNC; undef; } }, $c; } sub destroy { Pe "pkg %s(%s) destroyed", $cnt, EhV $_[0], cnt; undef } sub DESTROY { goto &destroy } } package main; sub callfunc(;$$); #silence warn about proto not ready sub callfunc (;$$) { my $p = pkg->new; my ($callfunc, $recur) = @_; $callfunc||=0; $recur||=0; local * FUNC #; *FUNC # change line = sub () { Pe "In FUNC, cnt=%s", $p->{cnt}; undef }; FUNC() if $callfunc; callfunc($callfunc, $recur) if $recur && $recur--; } Pe "** no recursion"; callfunc; callfunc 1; Pe "** split define/assign recursion"; callfunc 0,1; callfunc 1,1; pkg::destroy #### ** no recursion pkg 1 created pkg 1(1) destroyed pkg 2 created In FUNC, cnt=2 pkg 2(2) destroyed ** split define/assign recursion pkg 3 created pkg 4 created Subroutine main::FUNC redefined at /tmp/tst2.pl line 29. pkg 4(4) destroyed pkg 4(3) destroyed pkg 5 created In FUNC, cnt=5 pkg 6 created Subroutine main::FUNC redefined at /tmp/tst2.pl line 29. In FUNC, cnt=6 pkg 6(6) destroyed pkg 6(5) destroyed pkg 6(∄) destroyed #### ** no recursion pkg 1 created pkg 1(1) destroyed pkg 2 created In FUNC, cnt=2 pkg 2(2) destroyed ** split define/assign recursion pkg 3 created pkg 4 created pkg 4(4) destroyed pkg 4(3) destroyed pkg 5 created In FUNC, cnt=5 pkg 6 created In FUNC, cnt=6 pkg 6(6) destroyed pkg 6(5) destroyed pkg 6(∄) destroyed