Importer::Zim::Unit - Import functions with compilation unit scope
version 0.4.0
use Importer::Zim::Unit 'Scalar::Util' => 'blessed'; use Importer::Zim::Unit 'Scalar::Util' => ( 'blessed' => { -as => 'typeof' } ); use Importer::Zim::Unit 'Mango::BSON' => ':bson'; use Importer::Zim::Unit 'Foo' => { -version => '3.0' } => 'foo'; use Importer::Zim::Unit 'SpaceTime::Machine' => [qw(robot rubber_pig)];
"I'm gonna roll around on the floor for a while. KAY?" – GIR
This is a backend for Importer::Zim which makes imported symbols available during compilation.
Unlike Importer::Zim::Lexical, it works for perls before 5.18. Unlike Importer::Zim::Lexical which plays with lexical subs, this meddles with the symbol tables for a (hopefully short) time interval.
The statement
use Importer::Zim::Unit 'Foo' => 'foo';
works sort of
use Sub::Replace; my $_OLD_SUBS; BEGIN { require Foo; $_OLD_SUBS = Sub::Replace::sub_replace('foo' => \&Foo::foo); } UNITCHECK { Sub::Replace::sub_replace($_OLD_SUBS); }
That means:
Imported subroutines are installed into the caller namespace at compile time.
Imported subroutines are cleaned up just after the unit which defined them has been compiled.
See "BEGIN, UNITCHECK, CHECK, INIT and END" in perlsub for the concept of "compilation unit" which is relevant here.
See Sub::Replace for a few gotchas about why this is not simply done with Perl statements such as
*foo = \&Foo::foo;
You can set the IMPORTER_ZIM_DEBUG environment variable for get some diagnostics information printed to STDERR.
IMPORTER_ZIM_DEBUG
STDERR
IMPORTER_ZIM_DEBUG=1
Importer::Zim
"BEGIN, UNITCHECK, CHECK, INIT and END" in perlsub
Importer::Zim::Lexical
Importer::Zim::EndOfScope
Adriano Ferreira <ferreira@cpan.org>
This software is copyright (c) 2017 by Adriano Ferreira.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Importer::Zim::Unit, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Importer::Zim::Unit
CPAN shell
perl -MCPAN -e shell install Importer::Zim::Unit
For more information on module installation, please visit the detailed CPAN module installation guide.