in reply to Re^5: hrm, Eval::Compile
in thread hrm, Eval::Compile
The trick is to delay the decision and to compile code for "both" (resp. all) possibilities. See this Perl code:
#!perl -w use strict; sub do_foo { print "Foo with $_[0]"; }; sub do_bar { print "Bar with $_[0]"; }; my %dispatch = ( foo => \&do_foo, bar => \&do_bar, ); my $method = rand > 0.5 ? 'foo' : 'bar'; $dispatch{ $method }->('Test');
This code does not know whether $method will contain foo or bar but it can run the code for either subroutine.
This technique of calling code through subroutine references is discussed and widely employed in Higher Order Perl.
|
|---|