Brothers in perl,
here is a little module that i like to discuss. It is a workaround to the issue that INIT-Blocks are not executed in an evaled environment like mod_perl and WSH (Windows Scripting Host). Everything else you need to know is in the pod.
Do you thinks itīs well written? do you detect any caveats? what about the name?
eager to hear the monks wisdom,
holli
here is a little module that i like to discuss. It is a workaround to the issue that INIT-Blocks are not executed in an evaled environment like mod_perl and WSH (Windows Scripting Host). Everything else you need to know is in the pod.
Do you thinks itīs well written? do you detect any caveats? what about the name?
eager to hear the monks wisdom,
holli
package Evaled::INIT; use 5.008; use strict; use warnings; our $VERSION = '0.01'; my $filter; use Filter::Simple sub { return if $filter++; my $i=0; while ( $_ =~ s/(^|[\s\t;}])INIT(?!\x00)([\s\n]*|{)/$1INIT\x00 { \ +&SINIT$i; } \nsub SINIT$i$2/ms ) {$i++;}; s/\x00//msg; }; 1; __END__ =head1 NAME Evaled::INIT - Perl extension for executing INIT-sections =head1 SYNOPSIS use Evaled::INIT; =head1 ABSTRACT When you I<use> a module in an I<eval()>-statement, then the modules I<INIT>-sections (if any) are not executed. This modu +le can be used to work-around this behaviour. =head2 VERSION This is Version 0.1, early beta-state ,-) =head1 DESCRIPTION When you I<use> a module in an I<eval()>-statement, like $mod = "Foo"; eval "use $mod;"; then the modules I<INIT>-sections (if any) are not executed. Such statements may be used to load unknown modules at runtime, or might be implicitly caused by an environment like the I<Windows Scr +ipting Host (WSH)> or mod_perl. Such a WSH-PerlSript is always executed within an I<eval()>. This behaviour can cause the module to act irregularly. If you need to use such a module in any I<eval>-statement you can you +use this module to work around. Simply add the line use Evaled::INIT; at the top of the modules code (right after the I<package xxx;>-statem +ent). I<Evaled::INIT> acts as a source-filter. B<Hint:> I<A source-filter is a module that can change the sourcecode of the mo +dule that uses it to whatever the filter wants. This happens before the modules code is com +piled.> In this case the filter will replace all INIT { #do something } with INIT { &SINITx(); } sub SINITx { #do something } where I<x> is the number of the I<BEGIN>-section (there can be more th +an one) in top-down-order, starting with 0. After that conversion you can call the I<SINITx>-Functions as apropria +te, like $mod = "Foo"; eval "use $mod;"; eval "&$mod::SINITx();"; =head2 EXPORT None by default or query. =head2 EXAMPLES =head3 Bad example, INIT is not executed. #File t.pm INIT { print "INIT\n"; } 1; #File t.pl eval "use t.pm;"; print "main\n"; Output: >>perl t.pl main >> =head3 Good example, INIT can be called. #File t.pm use Evaled::INIT; INIT { print "INIT\n"; } 1; #File t.pl eval "use t.pm; &t::SINIT0();"; print "main\n"; Output: >>perl t.pl INIT main >> =head3 Good example, INIT still works #of course the module will still work without eval #File t.pm use Evaled::INIT; INIT { print "INIT\n"; } 1; #File t.pl use t.pm; print "main\n"; Output: >>perl t.pl INIT main >> =head1 AUTHOR Markus Holzer (m.holzer/at/kvsaarland.de) =head1 COPYRIGHT AND LICENSE This library is free software; you can redistribute it and/or modify i +t under the same terms as Perl itself. =cut
Retitled by davido.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Evaled::INIT - allow init blocks to execute inside eval block
by Juerd (Abbot) on Jan 11, 2005 at 18:48 UTC | |
by holli (Abbot) on Jan 12, 2005 at 08:04 UTC | |
Re: Evaled::INIT - allow init blocks to execute inside eval block
by dragonchild (Archbishop) on Jan 11, 2005 at 18:06 UTC | |
by gaal (Parson) on Jan 11, 2005 at 18:38 UTC | |
Re: Evaled::INIT - allow init blocks to execute inside eval block
by PodMaster (Abbot) on Jan 12, 2005 at 07:42 UTC | |
by holli (Abbot) on Jan 12, 2005 at 08:03 UTC | |
by PodMaster (Abbot) on Jan 12, 2005 at 08:33 UTC |
Back to
Meditations