Suppose you want to be able to build a function on the fly (as a closure) then call it like any other subroutine. Your answer is to modify the symbol table, to wit: $sub1 = sub { print "Hi!\n";};$clo2 = sub { print "OK?\n"; local *sub1 = $sub1; sub1();};$clo2->();sub1();Here, the call to $clo2 will print "OK?", then "Hi!", then the second attempt to call sub1() will fail because it's not defined...
No comments:
Post a Comment