Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: A lexical lib pragma?

by shmem (Chancellor)
on Jan 04, 2020 at 16:54 UTC ( [id://11110949]=note: print w/replies, xml ) Need Help??


in reply to A lexical lib pragma?

I wonder how hard would it be to create a lexical version of the lib pragma.

Short answer: very hard, since restoring @INC from a lexical must be done at compile time. But without a BEGIN block, this lexical is set at runtime. So Padwalker maybe, or some wizardry which works on the code which is currently being compiled - if such a beast exists at all.

I would go the route of the if module:

use lib::lexical MODULE, PATHSPEC;

or even

use lib::lexical modules => MODULESPEC, path => PATHSPEC;

without modifying @INC at all in the calling code, and leave that to the module loader lib::lexical, which localizes @INC. So, for the first usage, something like this:

package lib::lexical; our @SAVEINC = @INC; our @LOCALINC = @INC; sub work { my $flag = shift(); my $method = $flag ? 'import' : 'unimport'; my $p = shift; # PACKAGE if (! $p and ! $flag) { # plain 'no lib::lexical;' @LOCALINC = @SAVEINC unless $p; # restore to original @INC return; } if ($flag) { # prepend @paths to @INC unshift @LOCALINC, @_; } else { # weed out @paths from @INC my %i; @i{@_} = (1) x @_; @LOCALINC = grep { ! $i{$_} } @LOCALINC; } local @INC = @LOCALINC; warn $_,$/ for @INC; (my $file = "$p.pm") =~ s!::!/!g; require $file; my $m = $p->can($method); goto &$m if $m; } sub import { shift; unshift @_, 1; goto &work } sub unimport { shift; unshift @_, 0; goto &work } 1; __END__

Wait, what - this is (roughly) just what the lib module does? Yes of course! But I'd like to have

no lib;

just restore the original @INC, so lib::unimport should be:

sub unimport { shift; @INC = @ORIG_INC and return unless @_; # patch for the lazy <--- H +ERE my %names; foreach my $path (@_) { my($arch_auto_dir, $arch_dir, $version_dir, $version_arch_dir) = _get_dirs($path); ++$names{$path}; ++$names{$arch_dir} if -d $arch_auto_dir; ++$names{$version_dir} if -d $version_dir; ++$names{$version_arch_dir} if -d $version_arch_dir; } # Remove ALL instances of each named directory. @INC = grep { !exists $names{$_} } @INC; return; }

And then, the lib module modifies @INC globally, whereas the above module just uses a modified @INC for one use call (and the use calls which this call triggers)/.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: A lexical lib pragma?
by perlancar (Hermit) on Jan 05, 2020 at 05:10 UTC
    Thanks for this! Will study this further.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11110949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found