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

My program like this,but it is not work.Perl report "Can't locate object method "dirname" via package" . I think it is because BEGIN is running before any other perl statement.

And I don't know which is the script directory because it is running as cron job. So I can't use  use lib "./lib" too. So how can I use my own lib if I don't want edit PERL5LIB enviorment?

thanks!
use File::Basename; use My:Ownmodule; BEGIN { my $cwd = dirname $0; my $mylib = $cwd."/lib"; unshift (@INC,$mylib); }

Replies are listed 'Best First'.
Re: newbie question: can't use my own lib in crob job
by graff (Chancellor) on Mar 30, 2004 at 01:58 UTC
    You could try:
    use File::Basename; my $cwd = dirname $0; my $libpath = $cwd."/lib"; require "$libpath/My/Ownmodule.pm";
    I'd have to do some research to figure out how Perl handles a "BEGIN" block in relation to "use" for loading modules. (Others here presumably know this by heart and can point to relevant nodes at the monastery). But based on the evidence in your post, I'd say that it's trying to run your BEGIN block before it has loaded File::Basename. (Maybe putting "require File::Basename;" inside the BEGIN block would solve the problem too.)

    In any case, the approach shown above would avoid the issue, by using "require" to load your personal module from the intended path, and this is a run-time operation as opposed to a compile-time issue (or something to that effect).

      I try
      use File::Basename; my $cwd = dirname $0; my $libpath = $cwd."/lib"; require "$libpath/My/Ownmodule.pm";

      it is work normally. And putting "require File::Basename;" inside the BEGIN block can't solve the problem

      What is the difference between "require" and "use" ? Are there any disadvantage to use "require" to replace "use" ?

      I am in doubt whether there are any other method can make me to use  "use lib "xxxx"

      thanks!

        What is the difference between "require" and "use" ? Are there any disadvantage to use "require" to replace "use" ?
        perldoc -f use
        perldoc -f require

        I am in doubt whether there are any other method can make me to use "use lib "xxxx"
        Sorry, can you translate that to English?
Re: newbie question: can't use my own lib in crob job
by The Mad Hatter (Priest) on Mar 30, 2004 at 01:48 UTC
    How about just
    use lib '/full/path/to/perllib'; use My:Ownmodule;

      I don't know the exactly full path of the lib because I can't force other user install the lib to the desired path.

      thanks!

        Are you concerned about making this easy to install for other users? While that's a good thing, I think you're seriously overthinking things. If users have to set up a cron job to make this work, they'll have to edit code anyway. Just tell them to install the module via the normal mechanism or add a use lib line to their code.

        (As a side note, prepending "newbie" to your post subjects is useless; it adds no information of value.)

Re: newbie question: can't use my own lib in crob job
by Plankton (Vicar) on Mar 30, 2004 at 03:12 UTC
    Why don't you want to edit your PERL5LIB? Set it in your crontab ...
    * 6 * * * PERL5LIB=/whatever/you/need/it/to/be ; export PERL5LIB ; you +r_script.pl

    Plankton: 1% Evil, 99% Hot Gas.

      thanks for your reply .

      I just want to do it in my script. And I don't know I can write this command in cron tab before you tell me. I am newbie for unix and perl :-) But I still in doubt whether I can do this in script .

      thanks again!