how to use the pm in other location instead of current one.

Discussion in 'Perl Misc' started by Franklin Lee, Feb 23, 2004.

  1. Franklin Lee

    Franklin Lee Guest

    Hi All,

    Now I have some pl and pm files in /opt/myapp/bin.
    But some pm (the same name with previous) may exit dir /opt/myapp/newbin.
    If this dir exist, I want to use the pm in it.

    So I write below codes:
    /opt/myapp/bin/app.pl
    **********************************************
    BEGIN {
    if(-r /opt/myapp/newbin) {
    unshift(@INC, "/opt/myapp/newbin");
    } else {
    unshift(@INC, "/opt/myapp/bin");
    }
    }

    #my code begin....
    require "mymodule.pm";

    use strict;
    .....
    ***************************************************

    It seems work. The program load the PM in /opt/myapp/newbin/mymodule.pm.
    But I don't see this kind of example some where.

    So I want to know if it is a good one orthere exist better solution?

    Thank you!

    Franklin
    Franklin Lee, Feb 23, 2004
    #1
    1. Advertising

  2. Franklin Lee

    Anno Siegel Guest

    Franklin Lee <> wrote in comp.lang.perl.misc:
    > Hi All,
    >
    > Now I have some pl and pm files in /opt/myapp/bin.
    > But some pm (the same name with previous) may exit dir /opt/myapp/newbin.
    > If this dir exist, I want to use the pm in it.
    >
    > So I write below codes:
    > /opt/myapp/bin/app.pl
    > **********************************************
    > BEGIN {
    > if(-r /opt/myapp/newbin) {


    Missing quotes. This would result in a syntax error. Please copy/paste
    code, don't re-type it.

    > unshift(@INC, "/opt/myapp/newbin");
    > } else {
    > unshift(@INC, "/opt/myapp/bin");
    > }
    > }
    >
    > #my code begin....
    > require "mymodule.pm";
    >
    > use strict;
    > ....
    > ***************************************************
    >
    > It seems work. The program load the PM in /opt/myapp/newbin/mymodule.pm.
    > But I don't see this kind of example some where.
    >
    > So I want to know if it is a good one orthere exist better solution?


    You can "use lib" instead, which saves you the BEGIN block:

    use lib -r '/opt/myapp/newbin' ? '/opt/myapp/newbin' : '/opt/myapp/bin';

    The expression after "lib" is evaluated at compile time, it piggybacks on
    the compile-time execution of "use", as it were.

    Anno
    Anno Siegel, Feb 23, 2004
    #2
    1. Advertising

  3. Anno Siegel wrote:
    > Franklin Lee <> wrote in comp.lang.perl.misc:
    >> Now I have some pl and pm files in /opt/myapp/bin.
    >> But some pm (the same name with previous) may exit dir
    >> /opt/myapp/newbin.
    >> If this dir exist, I want to use the pm in it.


    <snip>

    >> It seems work. The program load the PM in
    >> /opt/myapp/newbin/mymodule.pm. But I don't see this kind of
    >> example some where.
    >>
    >> So I want to know if it is a good one orthere exist better
    >> solution?

    >
    > You can "use lib" instead, which saves you the BEGIN block:
    >
    > use lib -r '/opt/myapp/newbin' ? '/opt/myapp/newbin' : '/opt/myapp/bin';
    >
    > The expression after "lib" is evaluated at compile time, it
    > piggybacks on the compile-time execution of "use", as it were.


    Or an even easier method:

    use lib qw( /opt/myapp/newbin /opt/myapp/bin );

    That will make '/opt/myapp/newbin' the first and '/opt/myapp/bin' the
    second element in @INC, and since Perl uses the first path where it
    finds the module you are use()ing or require()ing, it should give the
    desired result.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 23, 2004
    #3
  4. Franklin Lee

    Franklin Lee Guest

    Thank you all.

    I make it more clear.
    When begining, first.pl in /opt/myapp/bin will be run.
    There exist mylib.pm under /opt/myapp/bin.
    But if dir /opt/myapp/newbin exist, I want to load mylib.pm
    /opt/myapp/newbin instead of the one under /opt/myapp/bin.

    So I use my BEGIN block. And I think it's not one good solution.

    For @INC, there will be current dir ".", and I want to first.pl search
    in /opt/myapp/newbin earlier than current dir "." (/opt/myapp/bin).

    Franklin
    Franklin Lee, Feb 24, 2004
    #4
  5. Franklin Lee wrote:
    > I make it more clear.
    > When begining, first.pl in /opt/myapp/bin will be run.
    > There exist mylib.pm under /opt/myapp/bin.
    > But if dir /opt/myapp/newbin exist, I want to load mylib.pm
    > /opt/myapp/newbin instead of the one under /opt/myapp/bin.
    >
    > So I use my BEGIN block. And I think it's not one good solution.


    Did you try any of the suggestions that were posted?

    Btw, if @INC includes the current directory, this should be sufficient
    to achieve the desired behaviour:

    use lib '/opt/myapp/newbin';

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 24, 2004
    #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. SFX
    Replies:
    15
    Views:
    761
  2. Replies:
    0
    Views:
    478
  3. Mike
    Replies:
    0
    Views:
    102
  4. Thufir
    Replies:
    1
    Views:
    89
    Thufir
    Sep 20, 2009
  5. Replies:
    1
    Views:
    276
    Thomas 'PointedEars' Lahn
    Mar 19, 2008
Loading...

Share This Page