×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to run perl code from anywhere

How to run perl code from anywhere

How to run perl code from anywhere

(OP)
Hi Experts,

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

CODE

[VM] perl 25 % pwd
/tmp/perl
[VM] perl 26 % ls -l *
-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

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

use strict;

use lib "./lib";
use utils;

print "$0 ... calling utils.pm..my_method()\n";
utils::my_method();

# 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.

exit;

[VM] perl 28 % cat ./lib/utils.pm
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;
[VM] perl 29 % ./run.pl
./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

[VM] tmp 32 % pwd
/tmp

[VM] tmp 33 % ./perl/run.pl
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!

RE: How to run perl code from anywhere

Try using the fullpath to lib:

CODE

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

Chris

RE: How to run perl code from anywhere

(OP)
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.

RE: How to run perl code from anywhere

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

RE: How to run perl code from anywhere

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

RE: How to run perl code from anywhere

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

The solutions work very well.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close