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

F

Franklin Lee

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
 
A

Anno Siegel

Franklin Lee said:
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
 
G

Gunnar Hjalmarsson

Anno said:
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.
 
F

Franklin Lee

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
 
G

Gunnar Hjalmarsson

Franklin said:
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';
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top