-2

How to load Perl modules using non-standard directories?

I download the trace module from cpan website and put into non-standard directories(/home/nrama/perl-script). but It doesn't taking my non-standard directories while executing below script. please let me know how to resolve this issue.

Trace module URL:

https://metacpan.org/pod/release/JV/Debug-Trace-0.05/lib/Debug/Trace.pm

Error:

syntax error at shift.pl line 3, near "use Trace." Execution of shift.pl aborted due to compilation errors.

Sample script:-

use strict;
use lib ("/home/nrama/perl-script");
use Trace.pm;

func('Nataraj', 'vino', 'mano' );
sub func {
my $name_1 = shift;
my $name_2 = shift;
my $name_3 = shift;
print "say hello to $name_1 $name_2 $name_3\n";
}

Note: Using perl, v5.6.0

ikegami
  • 367,544
  • 15
  • 269
  • 518
Nataraj
  • 852
  • 2
  • 14
  • 29
  • 2
    When you say _downloaded_, what do you mean? What exactly did you do? – simbabque Jul 04 '18 at 12:36
  • How to use trace module in my script? – Nataraj Jul 04 '18 at 12:39
  • 2
    Off-topic. Typo. The problem has nothing to do with the path. When you `use` a module, you need to use the module name, not the file name. `use Debug::Trace`. – Quentin Jul 04 '18 at 12:41
  • Can't locate Debug/Trace.pm in @INC (@INC contains: /home/nrama/perl-script /usr/local/lib/perl5/5.6.0/sun4-solaris /usr/local/lib/perl5/5.6.0 /usr/local/lib/perl5/site_perl/5.6.0/sun4-solaris /usr/local/lib/perl5/site_perl/5.6.0 /usr/local/lib/perl5/site_perl .) at shift.pl line 3. – Nataraj Jul 04 '18 at 12:43
  • I am getting an error used the Debug::Trace – Nataraj Jul 04 '18 at 12:43
  • 2
    Offtopic: Perl 5.6.0? Released 22nd March 2000? – dgw Jul 04 '18 at 12:46
  • Yes, correct. because of Our application using old Perl version. – Nataraj Jul 04 '18 at 12:47
  • 1
    Right now you have a syntax error here: `use Trace.pm;`. You are trying to concatenate barewords. Try `use Trace;` or rather `use Debug::Trace` (you need to preserve the directory structure for the latter). – eyevan Jul 04 '18 at 12:56
  • I am slightly modified my script even doesn't work also updated my script. – Nataraj Jul 04 '18 at 13:04
  • Can't locate Debug/Trace.pm in @INC (@INC contains: /home/nra/perl-script /usr/local/lib/perl5/5.6.0/sun4-solaris /usr/local/lib/perl5/5.6.0 /usr/local/lib/perl5/site_perl/5.6.0/sun4-solaris /usr/local/lib/perl5/site_perl/5.6.0 /usr/local/lib/perl5/site_perl .) at shift.pl line 4. BEGIN failed--compilation aborted at shift.pl line 4 – Nataraj Jul 04 '18 at 13:04
  • The script is called `Debug::Trace`, so the `Trace.pm` file needs to be in a folder called `Debug`, ie: `/home/nra/perl-script/Debug/Trace.pm`. Then with your script in `/home/nra/perl-script` it should work. – elcaro Jul 04 '18 at 13:10
  • I can execute my script(shift.pl). But doesn't show the trace output. my script output --> say hello to Nataraj vino mano – Nataraj Jul 04 '18 at 13:19
  • Please help me on this matter. – Nataraj Jul 04 '18 at 13:51
  • Do you mean that you put the module in your home directory that you show? Then try `use lib '/home/nrama/perl-script';` then `use Debug::Trace;` – zdim Jul 04 '18 at 18:31
  • Where is the script located? Is it being executed by the *nramamoorthy* user or some other user? If by another user, does that user have access to where the module is located?? – Ron Bergin Jul 04 '18 at 18:47
  • **I can execute my script(shift.pl). But doesn't show the trace output.** Does that mean that it's no longer giving you the "Can't Locate Debug/Trace.pm" error but it's also not giving you the trace output? – Ron Bergin Jul 04 '18 at 18:53

1 Answers1

3
use Trace.pm;

is not valid Perl, thus the syntax error. That should be

use Debug::Trace;

Furthermore, it makes no sense to harcode an absolute path in a script. You should remove

use lib ("/home/nrama/perl-script");

and either set env var PERL5LIB to that path in the login script

export PERL5LIB="$HOME/perl-script"

or replace it with a relative path to the script such as

use FindBin qw( $RealBin );
use lib $RealBin;

or

use FindBin qw( $RealBin );
use lib "$RealBin/lib";
ikegami
  • 367,544
  • 15
  • 269
  • 518