<?xml version="1.0" encoding="windows-1252"?>
<node id="421351" title="Evaled::INIT - allow init blocks to execute inside eval block" created="2005-01-11 12:58:47" updated="2005-08-15 13:47:34">
<type id="120">
perlmeditation</type>
<author id="241598">
holli</author>
<data>
<field name="doctext">
Brothers in perl,&lt;br&gt;
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 &lt;code&gt;eval&lt;/code&gt;ed environment like mod_perl and WSH (Windows Scripting Host). Everything else you need to know is in the pod.&lt;br&gt;
Do you thinks itīs well written? do you detect any caveats? what about the name?&lt;br&gt;&lt;br&gt;
eager to hear the monks wisdom,&lt;br&gt;
holli

&lt;readmore&gt;
&lt;code&gt;
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 { \&amp;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&lt;use&gt; a module in an I&lt;eval()&gt;-statement,
then the modules I&lt;INIT&gt;-sections (if any) are not executed. This module can be used to work-around this behaviour.

=head2 VERSION

This is Version 0.1, early beta-state ,-)

=head1 DESCRIPTION

When you I&lt;use&gt; a module in an I&lt;eval()&gt;-statement, like

  $mod = "Foo";
  eval "use $mod;";

then the modules I&lt;INIT&gt;-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&lt;Windows Scripting Host (WSH)&gt; or mod_perl.
Such a WSH-PerlSript is always executed within an I&lt;eval()&gt;.

This behaviour can cause the module to act irregularly.
If you need to use such a module in any I&lt;eval&gt;-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&lt;package xxx;&gt;-statement).
I&lt;Evaled::INIT&gt; acts as a source-filter. 

B&lt;Hint:&gt;
I&lt;A source-filter is a module that can change the sourcecode of the module that uses it to 
whatever the filter wants. This happens before the modules code is compiled.&gt;

In this case the filter will replace all 
  
  INIT 
  { 
    #do something
  }

with 

  INIT 
  {
    &amp;SINITx();
  }
  
  sub SINITx
  { 
    #do something
  }

where I&lt;x&gt; is the number of the I&lt;BEGIN&gt;-section (there can be more than one) in top-down-order,
starting with 0.
After that conversion you can call the I&lt;SINITx&gt;-Functions as apropriate, like

  $mod = "Foo";
  eval "use $mod;";
  eval "&amp;$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:

  &gt;&gt;perl t.pl
  main
  &gt;&gt;

=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; &amp;t::SINIT0();";
  print "main\n";

Output:

  &gt;&gt;perl t.pl
  INIT
  main
  &gt;&gt;

=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:

  &gt;&gt;perl t.pl
  INIT
  main
  &gt;&gt;
  


=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 it under the same terms as Perl itself. 

=cut
&lt;/code&gt;

&lt;/readmore&gt;

&lt;p&gt;&lt;small&gt;Retitled by [davido].&lt;/small&gt;&lt;/p&gt;

</field>
</data>
</node>
