develooper Front page | perl.perl5.porters | Postings from August 2022

Pre-RFC: "repackage"

Thread Next
August 2, 2022 09:24
Pre-RFC: "repackage"
Message ID:
Hi all,

Traditionally, if you need to change the package your code is running in,
but you don't know the package until runtime (something which impacts any
code which relies on caller()), then you have a few unsatisfactory options.

    eval "package $package { \$object->method_which_uses_caller }";

With the above, we lose `strict` and if `$object` is misspelled, we don't
find out until runtime instead of compile-time (and that's if we remember
to check $@). Other syntax errors have the same issue.

Or use (untested by me, but uses string
eval internally):

    use Call::From qw( call_method_from );
    my $proxy = call_method_from($package);
    $object->$proxy( 'method_which_uses_caller') ;

Import::Into also does some interesting stuff, but it's targeting importing
and also uses a string eval.

I have had a number of times (used in dynamic code generation), where I'd
love to just do this:

    repackage $package {

And have a nice, clean syntax to support dynamic changing of the package
without trying to trick caller() or using a string eval. We also get
compile-time checking of syntax and undeclared variables.

CTO, All Around the World
World-class software development and consulting

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About