module locating

Discussion in 'Perl Misc' started by ivowel@gmail.com, Dec 8, 2006.

  1. Guest

    dear perl experts:

    assume main.pl uses module module.pm, and both sit in /home/users/dir/.
    I know I can add directories to library paths, but I don't like to
    hardcode such paths---especially, because I may later want to do "mv
    /home/users/dir /home/users/dirnew" and still be able to execute from
    another location call "$ perl /home/users/dirnew/main.pl" . my first
    attempt was

    (my $libd= $0) =~ s/(.*\/).*/$1/g;
    use lib "$libd";
    die "$0 $libd\n";

    tells me about an empty compile time value. Is there a recommended way
    to accomplish this?

    sincerely,

    /iaw
     
    , Dec 8, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > dear perl experts:
    >
    > assume main.pl uses module module.pm, and both sit in /home/users/dir/.
    > I know I can add directories to library paths, but I don't like to
    > hardcode such paths---especially, because I may later want to do "mv
    > /home/users/dir /home/users/dirnew" and still be able to execute from
    > another location call "$ perl /home/users/dirnew/main.pl" . my first
    > attempt was
    >
    > (my $libd= $0) =~ s/(.*\/).*/$1/g;
    > use lib "$libd";
    > die "$0 $libd\n";
    >
    > tells me about an empty compile time value. Is there a recommended way
    > to accomplish this?


    You need a strategically placed BEGIN block to get the variable defined
    at compile time before the use lib is compiled.

    my $libd;
    BEGIN {($libd= $0) =~ s/(.*\/).*/$1/g;}
    use lib "$libd";

    I'm not sure how this method is different from FindBin, which you may
    also want to look in to.


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Dec 8, 2006
    #2
    1. Advertising

  3. Ben Morrow Guest

    Quoth Sherm Pendley <>:
    >
    > $0 is just the filename of your script, not the full path.


    This is not true. $0 usually contains the path the script was invoked as
    (it depends on the OS exactly what get put in argv[0]), and FindBin uses
    $0 to do its job.

    Ben

    --
    Like all men in Babylon I have been a proconsul; like all, a slave ... During
    one lunar year, I have been declared invisible; I shrieked and was not heard,
    I stole my bread and was not decapitated.
    ~ ~ Jorge Luis Borges, 'The Babylon Lottery'
     
    Ben Morrow, Dec 9, 2006
    #3
  4. Guest

    many thanks for both solutions.
     
    , Dec 9, 2006
    #4
  5. On 08 Dec 2006 23:58:14 GMT, wrote:

    >You need a strategically placed BEGIN block to get the variable defined
    >at compile time before the use lib is compiled.
    >
    >my $libd;
    >BEGIN {($libd= $0) =~ s/(.*\/).*/$1/g;}
    >use lib "$libd";

    ^^^^^^^
    ^^^^^^^

    Incidentally, useless use of quotes.

    >I'm not sure how this method is different from FindBin, which you may
    >also want to look in to.


    Well, some respected Perl hackers think FindBin has shortcomings, and
    that there are more reliable alternatives along the lines of the OP's
    one. But the issue is controversial:

    http://perlmonks.org/?node_id=41213


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Dec 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hilton

    Locating assemblies in ASP.NET

    Hilton, Jul 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    386
    Hilton
    Jul 22, 2003
  2. Anne
    Replies:
    0
    Views:
    366
  3. J. Babe

    Locating Database

    J. Babe, Aug 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    398
    Jurjen de Groot
    Aug 26, 2003
  4. Johann Blake
    Replies:
    0
    Views:
    494
    Johann Blake
    Dec 17, 2003
  5. Niall
    Replies:
    6
    Views:
    237
    Andy Hassall
    Jul 3, 2005
Loading...

Share This Page