Sub-refs in @INC screwing with your modules

Aaron Crane perl at aaroncrane.co.uk
Thu Aug 6 09:44:24 BST 2009


On Wed, Aug 5, 2009 at 9:32 PM, Uri Guttman<uri at stemsystems.com> wrote:
> this works:
>
>  perl -le 'open my $fh, q{<}, \q{print q{bar}}; unshift @INC, sub {print q{foo}; $fh }; require baz'
>
> i tried putting the open inside the sub and i get this error:
>
> Recursive call to Perl_load_module in PerlIO_find_layer at -e line 1.
> BEGIN failed--compilation aborted.

Apparently open-from-a-string internally loads a module to do its magic:

  $ perl -le 'print scalar keys %INC; open my $fh, "<", \""; print for
keys %INC'
  0
  XSLoader.pm
  PerlIO/scalar.pm
  PerlIO.pm

So you can make this work by doing open-from-a-string at least once
before the @INC hook gets called:

  perl -le '{ open my $fh, q{<}, \q{} } unshift @INC, sub {print
q{foo}; open my $fh, q{<}, \q{bar}; $fh }; require bar'

-- 
Aaron Crane ** http://aaroncrane.co.uk/



More information about the london.pm mailing list