Premise

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™ ;-)

The proposal

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. I don't see many options for one to inadvertently modify $_[1];
  2. the rationale should be that this is a somewhat "advanced" anyway so if one is using it, then he's supposed to know what it's doing.

1 See e.g.:


In reply to RFC: feature proposal re code in @INC by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.