NAME

Importer::Zim::EndOfScope - Import functions with compilation block scope

VERSION

version 0.4.0

SYNOPSIS

use Importer::Zim::EndOfScope 'Scalar::Util' => 'blessed';
use Importer::Zim::EndOfScope 'Scalar::Util' =>
  ( 'blessed' => { -as => 'typeof' } );

use Importer::Zim::EndOfScope 'Mango::BSON' => ':bson'; 

use Importer::Zim::EndOfScope 'Foo' => { -version => '3.0' } => 'foo';

use Importer::Zim::EndOfScope 'SpaceTime::Machine' => [qw(robot rubber_pig)];

DESCRIPTION

"Wait a minute! What planet is this?"
  – Zim

This is a backend for Importer::Zim which makes imported symbols available during the compilation of the surrounding scope.

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. (This time interval should be even shorter than the one that applies to Importer::Zim::Unit.)

HOW IT WORKS

The statement

use Importer::Zim::EndOfScope 'Foo' => 'foo';

works sort of

use B::Hooks::EndOfScope;

my $_OLD_SUBS;
BEGIN {
    require Foo;
    $_OLD_SUBS = Sub::Replace::sub_replace('foo' => \&Foo::foo);
}

on_scope_end {
    Sub::Replace::sub_replace($_OLD_SUBS);
}

That means:

  • Imported subroutines are installed into the caller namespace at compile time.

  • Imported subroutines are cleaned up after perl finished compiling the surrounding scope.

See Sub::Replace for a few gotchas about why this is not simply done with Perl statements such as

*foo = \&Foo::foo;

DEBUGGING

You can set the IMPORTER_ZIM_DEBUG environment variable for get some diagnostics information printed to STDERR.

IMPORTER_ZIM_DEBUG=1

SEE ALSO

Importer::Zim

B::Hooks::EndOfScope

Importer::Zim::Lexical

AUTHOR

Adriano Ferreira <ferreira@cpan.org>

COPYRIGHT AND LICENSE

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.