Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: call a modules INIT-section (fix for Win32::API::Type)

by ihb (Deacon)
on Mar 20, 2005 at 12:45 UTC ( #441009=note: print w/replies, xml ) Need Help??

in reply to call a modules INIT-section

I took a glance at Win32::API::Type which is the module that uses INIT. It seems like the author has misunderstood the purpose of INIT blocks. There's no need for the code to execute at INIT time. With some hackery you can work around this. The idea goes like this: add an INC hook that if Win32::API::Type gets used it locates the file it wants to use, reads the file, removes the INIT, opens a filehandle to the modified source and returns the filehandle. Code-wise it can look like

use 5.008; use File::Slurp qw/ read_file /; unshift @INC => sub { return undef unless $_[1] eq 'Win32/API/'; my $f = your_favourite_module_locate_routine(...); my $src = read_file($f); $src =~ s/INIT (?={)//; open my $fh, '<', \$src or die "I have no idea if or why this would ever die, but anyw +ay: $!"; return $fh; };
As for locating which file to load there's a number of modules on CPAN that does that.


See perltoc if you don't know which perldoc to read!