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

Pre-RFC: "repackage"

Thread Next
From:
Ovid
Date:
August 2, 2022 09:24
Subject:
Pre-RFC: "repackage"
Message ID:
CA+M4CHve1JOu5AuBeaThrcBBnH+Q1bExPXdeBZFNAjt=FB3qRg@mail.gmail.com
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 https://metacpan.org/pod/Call::From (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 {
        $object->method_which_uses_caller;
    }

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.

Best,
Ovid
CTO, All Around the World
World-class software development and consulting
https://allaroundtheworld.fr/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About