|
NAMELexical::Import - clean imports from package-exporting modulesSYNOPSISuse Lexical::Import "Carp"; use Lexical::Import qw(Time::HiRes time sleep); use Lexical::Import qw(Fcntl-1.01 :flock); use Lexical::Import ( ["Carp"], [qw(Time::HiRes time sleep)], [qw(Fcntl-1.01 :flock)], ); DESCRIPTIONThis module allows functions and other items, from a separate module, to be imported into the lexical namespace (as implemented by Lexical::Var), when the exporting module exports non-lexically to a package in the traditional manner. This is a translation layer, to help code written in the new way to use modules written in the old way.A lexically-imported item takes effect from the end of the definition statement up to the end of the immediately enclosing block, except where it is shadowed within a nested block. This is the same lexical scoping that the "my", "our", and "state" keywords supply. Within its scope, any use of the single-part name of the item (e.g., "$foo") refers directly to that item, regardless of what is in any package. Explicitly package-qualified names (e.g., "$main::foo") still refer to the package. There is no conflict between a lexical name definition and the same name in any package. This mechanism only works on Perl 5.11.2 and later. Prior to that, it is impossible for lexical subroutine imports to work for bareword subroutine calls. (See "BUGS" in Lexical::Var for details.) Other kinds of lexical importing are possible on earlier Perls, but because this is such a critical kind of usage in most code, this module will ensure that it works, for convenience. If the limited lexical importing is desired on earlier Perls, use Lexical::Var directly. PACKAGE METHODSThese methods are meant to be invoked on the "Lexical::Import" package.
BUGSOnly scalars, arrays, hashes, and subroutines can be translated from the package namespace to the lexical namespace. If a module exports more exotic items, such as bareword I/O handles or formats, they will be lost.If an exporting module does anything more complex than just inserting items into the calling package, this is liable to fail. For example, if it records the name of the calling package for some functional purpose then this won't work as intended: it will get the name of a temporary package that doesn't exist once the importing is complete. If an exporting module tries to read a variable in the calling package, this will fail in two ways. Firstly, because it sees a temporary package, it won't pick up any variable from the real caller. Secondly, it is liable to bring the variable into existence (with an empty value), which looks like it exported the variable, so the empty variable will be lexically imported by the real caller. Subroutine calls, to lexically-imported subroutines, that have neither sigil nor parentheses (around the argument list) are subject to an ambiguity with indirect object syntax. If the first argument expression begins with a bareword or a scalar variable reference then the Perl parser is liable to interpret the call as an indirect method call. Normally this syntax would be interpreted as a subroutine call if the subroutine exists, but the parser doesn't look at lexically-defined subroutines for this purpose. The call interpretation can be forced by prefixing the first argument expression with a "+", or by wrapping the whole argument list in parentheses. If this package's "import" method is called from inside a string "eval" inside a "BEGIN" block, it does not have proper access to the compiling environment, and will complain that it is being invoked outside compilation. Calling from the body of a "require"d or "do"ed file causes the same problem. Other kinds of indirection within a "BEGIN" block, such as calling via a normal function, do not cause this problem. Ultimately this is a problem with the Perl core, and may change in a future version. SEE ALSOLexical::Var, Sub::ImportAUTHORAndrew Main (Zefram) <zefram@fysh.org>COPYRIGHTCopyright (C) 2010, 2011 Andrew Main (Zefram) <zefram@fysh.org>LICENSEThis module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |