Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Howto set path for local package in a Perl code

by neversaint (Deacon)
on Jun 01, 2006 at 09:38 UTC ( [id://553007]=perlquestion: print w/replies, xml ) Need Help??

neversaint has asked for the wisdom of the Perl Monks concerning the following question:

Dear Masters,

I have the following directory structure for storing my code and the personal package use by the code.
~/MyPerl | | |__ src/ |_ mycode.pl | |_ mypackage.pm
Currently in mycode.pl, I have:
use strict; use warnings; use lib "home/neversaint/MyPerl/src/mypackage.pm"; # the rest of my code
Although this gives no problem:
~/MyPerl $ perl -c src/mycode.pl mycode.pl syntax ok
But when I run the code it fails to recognize the subroutine which I kept in mypackage.pm
~/MyPerl $ perl src/mycode.pl some_arg Undefined subroutine &main::some_subrutine called at src/mycode.pl lin +e 10.
What's wrong with the way I set the path of my local package? How can overcome this problem?

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Howto set path for local package in a Perl code
by holli (Abbot) on Jun 01, 2006 at 09:48 UTC
    Hard to tell what's wrong in line 10 when all you show us are lines 1-4. Anyway this line
    use lib "home/neversaint/MyPerl/src/mypackage.pm";
    should read
    use lib "/home/neversaint/MyPerl/src"; use mypackage;
    The arguments for use lib are paths, not files.

    Also, you shouldn't name your modules in all lower case. That's reserved for pragmas.


    holli, /regexed monk/
      Dear holli,

      Thanks so much for the reply, however after following your suggestion with this header in mycode.pl
      use strict; use warnings; use lib "/home/neversaint/MyPerl/src"; use MYPACKAGE;
      what I get is this...
      ~/MyPerl/src $ perl -c src/mycode.pl # gives Can't locate MYPACKAGE.pm in @INC (@INC contains: home/neversaint/MyPe +rl/src /home/neversaint/lib/perl5/site_perl/5.8.5/i386-linux-thread-m +ulti/5.8.5/i386-linux-thread-multi
      In MYPACKAGE.pm what I have is this:
      # plain header nothing here... # straight go to the functions.... sub some_func { # do sth; return $soth } 1;
      Although I have no problem at all running mycode.pl in src/ directory : ~/MyPerl $ perl mycode.pl someparam

      Did I still miss anything?

      ---
      neversaint and everlastingly indebted.......
        Can't locate MYPACKAGE.pm in @INC (@INC contains: home/neversaint/MyPerl/src /home/neversaint/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi/5.8.5/i386-linux-thread-multi

        That error message still gives the path without the leading slash. Which makes me think that perhaps you haven't edited the file successfully to correct that.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        neversaint, try this code.
        This is for you script file 'mycode.plx'
        # In script file named: mycode.plx use strict; use warnings; use diagnostics; # This just gives you more info. use lib '/home/neversaint/MyPerl/src'; use MyPackage; # Now the rest of your code.
        This is for your package file 'MyPackage.pm'
        # In MyPackage.pm package MyPackage; use strict; use warnings; use diagnostics; sub some_function { print("My some function"); }
        Now here is the important part. The use statement, the file name, and the package declaration MUST all be in the same case.
        • Use statement: use MyPackage;
        • File Name: MyPackage.pm
        • Package statement: package MyPackage;
        Kristofer Hoch
Re: Howto set path for local package in a Perl code
by davorg (Chancellor) on Jun 01, 2006 at 09:49 UTC
    use lib "home/neversaint/MyPerl/src/mypackage.pm";

    That's almost certainly wrong. The path should probably start with a '/'. And use lib only contains paths to library directories. You need a separate use statement to actually load the module.

    use lib '/home/neversaint/MyPerl/src/'; use mypackage;

    (And, as holli says, you shouldn't give a module a lower case name)

    Alternatively, you might look at the FindBin module.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Example: Use FindBin and your script will work no matter where you move it and its modules together.
      use FindBin; use lib "$FindBin::Bin"; use MyPackage;
      use lib only adds the path to @INC, you still need to use MyPackage after it.
Re: Howto set path for local package in a Perl code
by marto (Cardinal) on Jun 01, 2006 at 09:52 UTC
Re: Howto set path for local package in a Perl code
by codeacrobat (Chaplain) on Jun 02, 2006 at 06:19 UTC
    In the shell:
    export PERL5LIB=/home/neversaint/MyPerl/src
    This way you need not to specify the module location inside your script.
Re: Howto set path for local package in a Perl code
by tcf03 (Deacon) on Jun 01, 2006 at 11:21 UTC
    you could do
    #!/usr/bin/perl use strict; use warnings; BEGIN { push @INC, '/your/directory'; }
    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
Re: Howto set path for local package in a Perl code
by BaldPenguin (Friar) on Jun 01, 2006 at 17:26 UTC
    I offer this as advice and as a question
    When faced with this situation, I have always used a flag in my shebang
    #!/path/to/perl -I.
    Are there issues with doing that?

    Don
    WHITEPAGES.COM | INC
    Everything I've learned in life can be summed up in a small perl script!
      Are there issues with doing that?
      It can easily fail:
      #!perl -I. #t.pl use strict; use warnings; use XTest; print "ok\n";
      -------
      #XTest.pm package XTest; use strict; use warnings; 1;
      -------
      F:\tmp\test>dir *.p* Directory Listing of F:\tmp\test 01.06.2006 20:02 52 t.pl 01.06.2006 20:02 46 XTest.pm 2 File(s 98 Bytes 0 Directories, 2.727.182.336 Bytes free F:\tmp\test>perl t.pl ok F:\tmp\test>cd .. F:\tmp>perl test\t.pl Can't locate XTest.pm in @INC (@INC contains: F:/Perl/lib F:/Perl/site +/lib .) at test\t.pl line 4. BEGIN failed--compilation aborted at test\t.pl line 4. F:\tmp>


      holli, /regexed monk/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://553007]
Approved by marto
Front-paged by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-23 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found