Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Dynamically Changing Packages w/out Eval

by duelafn (Parson)
on Jun 15, 2009 at 13:41 UTC ( [id://771658]=note: print w/replies, xml ) Need Help??


in reply to Dynamically Changing Packages w/out Eval

Well, everything is possible with a source filter :) (Sorry, only solution I could think of.)

package DynPackage; use Filter::Simple; use Text::Balanced qw/ extract_bracketed /; FILTER_ONLY code => sub { 1 while s/\bdyn_package \s+ ([^\n]+?) \s+ (\{.+)$ / do { my ($pkgcode, $stuff) = ($1,$2); my ($code, $rest) = extract_bracketed($stuff,'{}') +; qq| { \$DynPackage::pkg = $pkgcode; eval "package \$DynPackage::pkg;" . <<' DYNPACKAGE_AUTOBLOCK'; $code DYNPACKAGE_AUTOBLOCK } $rest |; } /xes }, # FOR DEBUGGING: # all => sub { print } ; 1;

Later on...

use DynPackage; foreach my $block ($self->get_blocks) { dyn_package $block->package { $block->setup->(); $block->code->(); $block->teardown->(); }; $self->_croak($@) if $@; }

Good Day,
    Dean

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://771658]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-06-19 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.