in reply to perl -M using a module in current dir

perl -Mlib=. -MFoo::Bar -e 'print barfoo("blah");'
should work. '=' allows you to pass an argument to the module so -Mlib=. is the same as
use lib '.';
which does an unshift for you but it does it at compile time and before the -MFoo::Bar happens.

Updated Broquaint makes a good point, Bar.pm must be the in Foo directory in . I was going to say also that -I. is wrong but it's not wrong it's just weird.

My first reaction was to suggest -I but when I checked it with

perl -I. -e 'print join("\n", @INC)'
I didn't see . at the start of @INC so I switched to -Mlib=. Then I looked again and I saw that -I. does not not put . at the start of the search path, for my perl, it puts
./5.8.3/i386-linux-thread-multi ./5.8.3 ./i386-linux-thread-multi ./5.8.2 ./5.8.1 ./5.8.0 .
which will probably have the same effect. Where is it documented that -Idir will add dir and 6 other variations of dir to the search path? Or is that just some weirdness from perl on Fedora Core 2?

Replies are listed 'Best First'.
Re^2: perl -M using a module in current dir
by electrosphere (Beadle) on Sep 23, 2004 at 12:03 UTC
    Thanks for both your replies.

    Yes, I was silly not to "back up one dir" to Foo's dir and try it. It was almays my understanding the -I. puts . to the end of the @INC search path. But now I think not. It seems that -I. and -Mlib=. will both work for me, but for each the . is ordered differently:

    $ perl -I. -e 'print join("\n", @INC)' ./5.8.3/i386-linux-thread-multi ./5.8.3 ./i386-linux-thread-multi ./5.8.2 ./5.8.1 ./5.8.0 . /usr/lib/perl5/5.8.3/i386-linux-thread-multi /usr/lib/perl5/5.8.3 /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3 <-- the other Foo::Bar package is here /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl $ perl -Mlib=. -e 'print join("\n", @INC)' . /usr/lib/perl5/5.8.3/i386-linux-thread-multi /usr/lib/perl5/5.8.3 /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3 <-- the other Foo::Bar package is here /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl

    I'm using FC2