Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to execute/call perl module on different path ?

by bh_perl (Monk)
on Jun 06, 2012 at 17:23 UTC ( [id://974790]=perlquestion: print w/replies, xml ) Need Help??

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


Hi

I have created/export some module from my MainProgram.pl. The program saved on /data/script directory. Below some sample from my perl module.

The problem is i only can execute the script when i go to /data/script directory only. I cannot execute it from others directory. Why?

Please help me how to execute the script outside from the specific directory ?
#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Time::ParseDate qw(parsedate); use Time::Local; use POSIX qw(strftime); use ConfigurationModule qw/set_directory get_date PARSE_CSV INTTOHEX/; use TrunkCategoryModule qw/read_trunk_category/; use CheckFilterModule qw/check_filter/; use CheckProductTypeModule qw/assign_bgid assign_mbi_final/; use CheckCallCategoryModule qw/assign_call_category/; use NormalizeFieldModule qw/normalize_bnumber date_format check_error_ +code/; use NormalizePointTargetModule qw/normalize_point_target/; use NormalizeInbillModule qw/get_total_record get_startdate get_lastda +te/; use TiuMappingModule qw/tiu_mapping/; ... ... ...

Replies are listed 'Best First'.
Re: How to execute/call perl module on different path ?
by kennethk (Abbot) on Jun 06, 2012 at 17:38 UTC
    This is a pathing issue. When trying to resolve modules, perl uses the module name to resolve what file to look in, and checks the directories in @INC. One of those entries is usually ., so when your modules are in the local path, you find them. You can address this issue in a couple ways:
    • You could use lib, which will modify @INC at compile time.
    • You could invoke perl with -I and explicitly include the path
    • You could modify your hashbang to explicitly include the path to your libraries, as per the previous solution
    • You could parse $0, perhaps with File::Basename, and explicitly add that to @INC. For that to work, it needs to be in a BEFORE block.
    All in all, you probably want the first option.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: How to execute/call perl module on different path ?
by brx (Pilgrim) on Jun 06, 2012 at 17:31 UTC

      Or, alternatively:

      #!/usr/bin/perl BEGIN { push( @INC, "/data/script" ); }

      Either one works, but the first is probably preferred.

      ~Thomas~
      confess( "I offer no guarantees on my code." );

        use lib $extra is (roughly) equivalent to BEGIN { unshift @INC,$extra }, so perl searches for libraries in $extra first. Your code

        BEGIN { push( @INC, "/data/script" ); }

        makes perl search for libraries in extra last, and only if no matching libraries were found in the original @INC.

        That's hardly the same, and while it "works" in this special case, it will probably break in other situations.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How to execute/call perl module on different path ?
by AnomalousMonk (Archbishop) on Jun 06, 2012 at 17:43 UTC

    lib is part of the Perl core (and has been for a long, long time), so it should not be necessary to download it.

Re: How to execute/call perl module on different path ?
by 2teez (Vicar) on Jun 06, 2012 at 18:44 UTC
    Hi,

    Ofcourse, my default answer would be use a

    use lib '....';
    as wonderful monks had adviced. However, you need know that if you have to move your application to other computer with different pathnames, you will have to change your use lib '....' in all the program files where such occurs.

    Just saying .....
    Look up perlmod and perlmodlib.
Re: How to execute/call perl module on different path ?
by clueless newbie (Curate) on Jun 06, 2012 at 20:24 UTC
    perl -I/data/script ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://974790]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-16 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found