[Chicago-talk] Creating dynamic, callable subroutines

Jay Strauss me at heyjay.com
Sun Aug 3 19:15:42 PDT 2008


Thanks Steve.  I already got it working like in the example.

On Sun, Aug 3, 2008 at 6:50 PM, Steven Lembark <lembark at wrkhors.com> wrote:
>
>> 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
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>


More information about the Chicago-talk mailing list