Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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
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.


In reply to Evaled::INIT - allow init blocks to execute inside eval block by holli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (6)
    As of 2016-05-29 17:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?