[Chicago-talk] Creating dynamic, callable subroutines
Steven Lembark
lembark at wrkhors.com
Sun Aug 3 16:50:54 PDT 2008
> How can I create the actual subroutines dynamically, so that they can
> be called from the outside using the normal:
>
> TickType();
Don't bother: if the only thing you are doing is calling
the other subs then just import them into your local
calling space:
use Symbol qw( qualify_to_ref );
# list of things you want in your local space
# by package and source sub; you can also supply
# an optional local name instead of importing the
# name as-is.
my %importz =
(
'Foo::com::ib::client' =>
{
Order => '',
TickType => ''
# if they called this something else
# in the last rev you can rename the
# local version.
ScannerSubscription => 'ScanSub',
},
)
# do the deed:
# walk down the packages, finding lists of subs to import.
# for each import, make a ref in this package with the
# dest name ( $src || $dst ), then assign it a coderef
# from the source package.
sub import_subs
{
while( my ( $package, $subz ) = each %importz )
{
while( my ( $src, $dst ) = each %$subz )
{
my $ref = qualify_to_ref ( $dst || $src ), __PACKAGE__;
*$ref = $package->can( $src )
or die "Oops: $package lacks '$src' to export!";
}
}
}
# viola! at this point calling Order in the local
# package will dispatch into the other package's
# 'Order' call.
--
Steven Lembark 85-09 90th St.
Workhorse Computing Woodhaven, NY, 11421
lembark at wrkhors.com +1 888 359 3508
More information about the Chicago-talk
mailing list