Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

perl -M using a module in current dir

by electrosphere (Beadle)
on Sep 23, 2004 at 11:14 UTC ( #393168=perlquestion: print w/replies, xml ) Need Help??
electrosphere has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I searched on PM first but found no joy on this specific piece of Perl wisdom, same for perldoc perlrun.

I have changes to a perl module that I want to quickly test from the command line, however perl -M appears to source the module installed in the perl system dir.

Assuming my module is Foo::Bar (package Foo::Bar), and I want to call a function barfoo() in it, I am currently doing:

perl -MFoo::Bar -e 'print barfoo("blah");'

I thought I could make it source the Module in the current dir by using -I. but it didn't work. I realized it would just append this to the end of the @INC array. I then included unshift(@INC, "."); to counter this but it still didn't work.

I'm sure there's a simple way! Thanks

Replies are listed 'Best First'.
Re: perl -M using a module in current dir
by broquaint (Abbot) on Sep 23, 2004 at 11:19 UTC
    You'll need to put in the Foo/ directory in the current directory if you wish to use the -M switch or use e.g
    shell> mkdir Foo shell> mv Foo/ shell> perl -I. -MFoo::Bar -e 'print barfoo("blah");'
    The -I. forces the current directory to be searched first.


Re: perl -M using a module in current dir
by fergal (Chaplain) on Sep 23, 2004 at 11:21 UTC
    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, 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?
      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

Re: perl -M using a module in current dir
by ikegami (Pope) on Sep 23, 2004 at 16:26 UTC

    Is in ./Foo? If not, just do
    perl -MBar -e 'print barfoo("blah");',
    even if it says package Foo::Bar inside.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://393168]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-02-18 05:19 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (250 votes). Check out past polls.