Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Dynamically Changing Packages w/out Eval

by duelafn (Parson)
on Jun 15, 2009 at 13:41 UTC ( #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? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2023-03-29 01:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (70 votes). Check out past polls.

    Notices?