One of my favourite 'tricks'1 is the possibility of putting code in @INC, as explained in perldoc -f require. Now, the standard way to use it is to return an open filehandle to the wanted module, e.g.
#!/usr/bin/perl use strict; use warnings; use lib sub { my $wanted=pop; return undef if $wanted ne 'Foo.pm'; open my $fh, '<', \<<'.EOM' or die "D'Oh! $!\n"; # ---- Begin package code. use strict; use warnings; package Foo; use base 'Exporter'; our @EXPORT='bar'; sub bar { print "Hmmm, seems to work!!\n"; } 1; __END__ .EOM # ---- End package code. $fh; }; use Foo; bar; __END__
(Although in this form looks more like obfuscation than a Good Thing™ ;-)
Now, it occurred to me that even if, as usual, there are many other ways to do what I'm about to propose, one may want to use this feature to "alias" a package (or better, a set of packages at a time) by modifying $_[1] (in the case of the sub or array form of this 'trick') - and returning undef to let the default mechanism continue the search in the standard locations. But this is not currently possible:
$ perl -le 'use lib sub {$_[1]="Foo.pm"; undef}; use Anything;' Modification of a read-only value attempted at -e line 1. BEGIN failed--compilation aborted at -e line 1.
So I wonder if $_[1] could be made not read-only instead.
I understand that there may be inherent risks in doing so, but
1 See e.g.:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: feature proposal re code in @INC
by diotalevi (Canon) on Jan 26, 2006 at 14:44 UTC | |
by blazar (Canon) on Jan 26, 2006 at 16:03 UTC | |
by Tanktalus (Canon) on Jan 26, 2006 at 17:34 UTC | |
by blazar (Canon) on Jan 26, 2006 at 18:36 UTC | |
by diotalevi (Canon) on Jan 26, 2006 at 17:22 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |