BassKozz has asked for the wisdom of the Perl Monks concerning the following question:

1st time poster (please take it easy on me)...

I outsourced some work on RentACoder.com a long time ago, and a programmer built me 3 perl scripts for various tasks. These scripts work flawlessly in XP (using activestate perl) but I have recently switched over to Ubuntu Linux for my full time computer use (bye, bye M$). But now I am having a few issues getting one of the scripts to run. I have very little experience with perl, but do have a rough general knowledge, and I have been able to correct a number of issues thanks to my good friend Google.com :-P, but I am stumped on this one.

The script just freezes in the beginning. Using the "-T" (tainting checks) attribute I get the following error message:

Can't locate Util.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl) at autocomp.pl line 9.
and here is line 9 of "autocomp.pl":

use Util;

But the "Util.pm" file is in the same directory as my script (autocomp.pl). Shouldn't Perl know to check in the directory where the script is located for the "Util.pm" file?

The funny thing is that there is another script that also calls Util.pm, and it works fine, so I am not sure if this is the real cause for the script not working or not?

TiA,
-BassKozz

Replies are listed 'Best First'.
Re: Script works in XP but not Ubuntu?
by ikegami (Patriarch) on Apr 01, 2009 at 21:43 UTC

    Perl never checks "the same directory as my script", but it usually* checks in ".".

    $ cat ../script.pl #!/usr/bin/perl use Foo; $ cat ../Foo.pm print("Foo!\n"); # In same dir as script 1; $ cat Foo.pm print("Foo?\n"); # In current dir 1; $ ../script.pl Foo?

    However, "." is not in @INC under -T.

    $ perl -le'print for @INC' /home/eric/lib/perl5/i486-linux-gnu-thread-multi <-- From PERL5LIB /home/eric/lib/perl5 <-- environ var /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl . $ perl -T -le'print for @INC' /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl

    If you don't care about this aspect of taint (i.e. if your script isn't setuid or sudo), then you can add "." to @INC explicitly by adding BEGIN { push @INC, '.' } to your script or by changing -T to -T -I. (including the ".").

    $ cat Foo.pm print("Foo!\n"); 1; $ perl -e'use Foo' Foo! $ perl -T -e'use Foo' Can't locate Foo.pm in @INC (@INC contains: ...) at -e line 1. BEGIN failed--compilation aborted at -e line 1. $ perl -T -I. -e'use Foo' Foo!

    * — For some unspecified definition of usual.

    Update: Added more examples.

Re: Script works in XP but not Ubuntu?
by CSJewell (Beadle) on Apr 01, 2009 at 21:51 UTC

    @INC would need to contain a . in order to load a module from the current directory. That's why it isn't loading. To add it is easy:

    Command line solution: perl -L. autocomp.pl

    Within-script solution: use lib '.';

      Within-script solution: use lib '.';

      In this case script will search libraries in cwd. If you want it to search them in the same directory as script itself you better use:

      use FindBin qw($Bin); use lib "$Bin";
        Thanks for all the feedback "use lib '.';" seems to have done the trick... but now I have another problem with this script, it seems to keep looping on the following function:

        #@data = SplitCSVLine($csv_line); sub SplitCSVLine { my ($line) = @_; my ($value, @data); while(1) { last if($line =~ /^\s*$/); if($line =~ /^[^\"]/) { $line =~ s/(.*?)(,|$ )//x; $value = $1; } else { $line =~ s/\"((\"\"|[^\"])*)\"(,|$ )//x; $value = $1; $value =~ s/\"\"/\"/g if defined $value; } push @data, $value; } return @data; }

        I can't figure out why, the code that calls this function is:

        sub GetStyleCodesMap { my @csv_lines = chrtoolbox::MyCat('style_codes.csv'); my $line_num = 0; foreach my $line (@csv_lines) { $line_num++; next if($line_num == 1); #skip the header last if($line eq "\n"); #done reading the mapping @data = SplitCSVLine($line);

        Any ideas?
        Thanks again for all the help,
        -BassKozz
Re: Script works in XP but not Ubuntu?
by didess (Sexton) on Apr 01, 2009 at 22:22 UTC
    Hi,
    indeed, current directory (".") is not checked by Perl to find Util.pm.
    (you see that clearly from the error message)
    One solution is to copy Util.pm into one of the directories listed in the answer.
    I think better to use /usr/local/lib/site_perl, because of its "local" nature:
    It should not be corrupted while upgrading the version or Perl in the future. (That's only an advice :-).
    You should check also the way Util.pm is spelled (UTIL.pm, ...) because Ubuntu's file systems are case-sensitive (not XP)
    Didess
Re: Script works in XP but not Ubuntu?
by ig (Vicar) on Apr 02, 2009 at 09:11 UTC