Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run perl code from anywhere

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
Hi Experts,

Please take a look at a small demo program including file structure below:

Code:
[b][VM] perl 25 % pwd[/b]
/tmp/perl
[b][VM] perl 26 % ls -l *[/b]
-rwxr-xr-x 1 test staff  133 2013-09-18 12:20 run.pl*

lib:
total 4
-rw-r--r-- 1 test staff 233 2013-09-18 12:20 utils.pm

[b][VM] perl 27 % cat run.pl[/b]
#!/usr/bin/perl

use strict;

use lib "./lib";
use utils;

print "$0 ... calling utils.pm..my_method()\n";
utils::my_method();
[b][COLOR=red]
# 1st Q: why do I need the name space 'utils::' here? Isn't my_method() a public method?
# If I did use the name space, e.g.
# &my_method(); ## This is line 10
# I'd get this error:
# Undefined subroutine &main::my_method called at ./run.pl line 10.
[/color][/b]
exit;

[b][VM] perl 28 % cat ./lib/utils.pm[/b]
package utils;

use strict;

our (@ISA, @EXPORT);
@ISA = qw(Exporter);
my @publicFunc = (
        'my_method',
        # more methods here
);

foreach (@publicFunc) {
        push @EXPORT, $_;
}

sub my_method {
        print "In utils.pm..my_method()\n";
}

1;
[b][VM] perl 29 % ./run.pl[/b]
./run.pl ... calling utils.pm..my_method()
In utils.pm..my_method()

So we know the executable 'run.pl' can be executed from /tmp/perl.

2nd Q: How do I make it runnable from anywhere?
Code:
[b][VM] tmp 32 % pwd[/b]
/tmp

[b][VM] tmp 33 % ./perl/run.pl[/b]
Can't locate utils.pm in @INC (@INC contains: ./lib /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at ./perl/run.pl line 6.
BEGIN failed--compilation aborted at ./perl/run.pl line 6.

Thanks!
 
Try using the fullpath to lib:

Code:
use lib "/tmp/perl/lib";
use utils;

Chris
 
Thank you, Chris.

But this would not work. In my example, I put ./perl under /tmp. In real world, different users may put ./perl under different places, for instance, you may put it under /etc, I may put it under /usr/local and he may put it under $HOME. Therefore, an absolute path will never work.
 
The way I use bespoke modules is putting them in the same place the perl script is running from and use this at the top of my scipts..

Code:
use FindBin qw($Bin);
use lib "$Bin";

The other alternative is package the module and install it like any other module, other than that perl cannot guess where the modules are, you cannot have a module that is in a random unknown location and expect it to work!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Hi,

As 1DMF states, one way or another the script will need to decifer the full path to the lib directory.

I personally create a custom environment variable named LIB (this can be described in setup instructions). Therefore I can then do the following in my scripts:

Code:
use lib $ENV{LIB} // die 'required environment variable LIB not defined';

Another alternative could be to execute a setup script, which could prompt users to supply the path to the lib, or grep the system for /lib/*.pm, which is then saved to disk for use.

Chris
 
Thank you both very much, DMF and Chris. I know, it's a long delayed "thank you".

The solutions work very well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top