Sub-refs in @INC screwing with your modules

Uri Guttman uri at
Wed Aug 5 21:32:39 BST 2009

>>>>> "DC" == David Cantrell <david at> writes:

  DC> As part of an Evil Plan, I am putting a sub-ref into @INC that will
  DC> screw around with modules as they're loaded.  Or at least, I'm trying
  DC> to.  I can't seem to get this to work:

  DC> perl -MIO::Scalar -e '
  DC>     unshift @INC, sub {
  DC>         print q{wibble};
  DC>         return IO::Scalar->new(\q{print q{wobble}})
  DC>     };
  DC>     eval "use foo"
  DC> '

  DC> which *should*, if I've RTFM correctly, print "wibblewobble".  But the
  DC> wobble never appears.  What am I doing wrong?

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:

perl -le 'unshift @INC, sub {print q{foo}; open my $fh, q{<}, \q{bar}; $fh }; require bar'
Recursive call to Perl_load_module in PerlIO_find_layer at -e line 1.
BEGIN failed--compilation aborted.

why would it recurse on the sub in @INC? it prints foo twice which shows


Uri Guttman  ------  uri at  -------- --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- ---------
---------  Gourmet Hot Cocoa Mix  ---- ---------

More information about the mailing list