Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

ADD directory to @INC using a string variable

by sid9559 (Novice)
on Sep 23, 2013 at 15:18 UTC ( #1055312=perlquestion: print w/ replies, xml ) Need Help??
sid9559 has asked for the wisdom of the Perl Monks concerning the following question:

I have a $path string variable that has the path for the directory I want to add to @INC I tried doing use lib $path; but it throws an error. Can someone please tell me the correct way to do it? Thanks, Sid

Comment on ADD directory to @INC using a string variable
Re: ADD directory to @INC using a string variable
by jellisii2 (Pilgrim) on Sep 23, 2013 at 15:21 UTC
      Thanks for your response but I want to use the path in a string variable. my $path = "/abc/xyz/bcd" use lib $path; it throws me the following error Empty compile time value given to use lib at CWD/filename.pm Thanks, Sid
Re: ADD directory to @INC using a string variable
by Laurent_R (Vicar) on Sep 23, 2013 at 15:28 UTC

    Try use lib hardcoded_path_name;. The use directives are executed at compile time, before the variables used in the code are set.

    Else you could set your variable in a BEGIN block, just prior to using it.

      I cannot use a hardcoded path, it is a varying path based on the user. $path = user's cwd/somepath/filename.pm Regards, Siddharth
        use lib './somepath/';
        or
        BEGIN { my $path = 'somepath'; push @INC, $path; }
        or why not just let the user set his/her PERL5LIB environment variable?
Re: ADD directory to @INC using a string variable
by toolic (Chancellor) on Sep 23, 2013 at 15:28 UTC
    • Show the code above your lib line.
    • Show your exact error message in "code" tags.
      Empty compile time value given to use lib at /proj/zn_ver_user2/sidsh +a/build_plugin/infra/lib/BuildPlugin.pm line 32. Use of uninitialized value in require at /proj/zn_ver_user2/sidsha/bui +ld_plugin/infra/lib/BuildPlugin.pm line 33. Use of uninitialized value in require at /proj/zn_ver_user2/sidsha/bui +ld_plugin/infra/lib/BuildPlugin.pm line 33. Can't locate BuildPluginInc.pm in @INC (@INC contains: /proj/zn_ver_u +ser2/sidsha/build_plugin/import/rndcfg/lib /proj/zn_ver_user2/sidsha/ +build_plugin/verif/lib /proj/zn_ver_user2/sidsha/build_plugin/infra/c +onfig /proj/zn_ver_user2/sidsha/build_plugin/infra/lib /proj/verif_re +lease_ro/vv/60/lib /proj/verif_release_ro/RegrPlatform/17/lib/perl /p +roj/verif_release_ro/ubts_access/56/lib/perl /proj/verif_release_ro/S +igSync/313/lib/perl /proj/verif_release_ro/s_trapbug/32/lib/perl /pro +j/verif_release_ro/psm/22/tb /proj/verif_release_ro/dsm/43/tb /proj/v +erif_release_ro/dfxcfg/3/lib/perl /proj/verif_release_ro/sql_utils/56 +/lib/perl/DBInsert /proj/verif_release_ro/sql_utils/56/lib/perl /proj +/verif_release_ro/kx_core/1678/lib/perl /proj/verif_release_ro/kx_too +ls/18/lib/perl /proj/verif_release_ro/patterndb/89/lib/perl /proj/ver +if_release_ro/DatabaseKeywords/131/lib/perl /proj/verif_release_ro/cx +shell/38/lib /proj/verif_release_ro/verif_modules/53/lib/perl /proj/v +erif_release_ro/bt/project_tools/242/lib/perl /proj/verif_release_ro/ +utils/63/lib/perl /proj/verif_release_ro/siteconfig/current/lib/perl +/proj/verif_release_ro/version_control/14/lib/perl /proj/verif_releas +e_ro/software_stack/12/lib/perl /proj/verif_release_ro/regr_support/2 +9/lib/perl /proj/verif_release_ro/regr_stack/65/lib/perl /proj/verif_ +release_ro/regr_load_balancer/40/lib/perl /proj/verif_release_ro/regr +_db_interface/102/lib/perl /proj/verif_release_ro/regr_config/109/lib +/perl /proj/verif_release_ro/regr_cluster_interface/34/lib/perl /proj +/verif_release_ro/crdb_api/117/lib/perl /proj/verif_release_ro/crdb20 +_db_interface/30/lib/perl /proj/verif_release_ro/crdb_db_interface/10 +5/lib/perl /proj/verif_release_ro/results_analysis/141/lib/perl /proj +/verif_release_ro/job_specification/195/lib/perl /proj/verif_release_ +ro/dispatch/266/lib/perl /proj/verif_release_ro/flow_managers/106/lib +/perl /proj/verif_release_ro/fm_verif_plugins/343/lib/perl /proj/veri +f_release_ro/autosmoke/89/lib /proj/zn_ver_user2/sidsha/build_plugin/ +out/linux_2.6.18_64.VCS/common/pub/lib/perl /proj/zn_ver_user2/sidsha +/build_plugin/out/linux_2.6.18_64.VCS/common/pub/bin /proj/verif_rele +ase_ro/rgb_build/196 /proj/verif_release_ro/rgb/70 /proj/verif_releas +e_ro/rgb/70/aleLexer/blib/lib /proj/verif_release_ro/leda_rules/2013. +Q2p1_2013.03-03/lib /proj/verif_release_ro/drop2cad/94/lib/perl /proj +/verif_release_ro/compression_utils/19/lib/perl /proj/verif_release_r +o/ustat_send/27/lib/perl /proj/verif_release_ro/project_config/25/lib +/perl /proj/verif_release_ro/p4w/223 /proj/verif_release_ro/cbwa/218/ +lib/perl /proj/verif_release_ro/xml_utils/47/lib/perl /proj/verif_rel +ease_ro/p4_mkwa/current /proj/verif_release_ro/error_reporting/25/lib +/perl /proj/verif_release_ro/cbwa_bootcore/current/lib/perl /tool/pan +dora64/.package/perl-5.8.8/lib/5.8.8/x86_64-linux-thread-multi /tool/ +pandora64/.package/perl-5.8.8/lib/5.8.8 /tool/pandora64/.package/perl +-5.8.8/lib/site_perl/5.8.8/x86_64-linux-thread-multi /tool/pandora64/ +.package/perl-5.8.8/lib/site_perl/5.8.8 /tool/pandora64/.package/perl +-5.8.8/lib/site_perl .) at /proj/zn_ver_user2/sidsha/build_plugin/inf +ra/lib/BuildPlugin.pm line 33. BEGIN failed--compilation aborted at /proj/zn_ver_user2/sidsha/build_p +lugin/infra/lib/BuildPlugin.pm line 33. Compilation failed in require at /proj/verif_release_ro/rgb_build/196/ +build line 8. BEGIN failed--compilation aborted at /proj/verif_release_ro/rgb_build/ +196/build line 8.
        If you run that through splain you'll get
        Empty compile time value given to use lib at /proj/zn_ver_user2/sidsh +a/build_plugin/infra/lib/BuildPlugin.pm line 32. Use of uninitialized value in require at /proj/zn_ver_user2/sidsha/build_plugin/infra/lib/BuildPlugin.p +m line 33 (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell y +ou the name of the variable (if any) that was undefined. In some cas +es it cannot do this, so it also tells you what operation you used th +e undefined value in. Note, however, that perl optimizes your progr +am anid the operation displayed in the warning may not necessarily ap +pear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program.

        I explain /demonstrate why its uninitialized in Re: ADD directory to @INC using a string variable (not when variables initialized use BEGIN)

        But I really just wanted to respond to say eew at your 60-entries of @INC, because whatever your reasons, 60-deep-@inc is eew :)

Re: ADD directory to @INC using a string variable (not when variables initialized use BEGIN)
by Anonymous Monk on Sep 23, 2013 at 15:38 UTC
      Thanks for the resources and links. I will take a look at them and see if I can make it work. Regards, Siddharth
Re: ADD directory to @INC using a string variable
by Laurent_R (Vicar) on Sep 23, 2013 at 15:54 UTC

    Assuming you are on some form of Un*x, can you use something like this:

    use lib '~/some_path/';

    BTW, the argument after lib should be a path.

      can I use a string variable instead ?? something like use lib $path Regards, Siddharth
Re: ADD directory to @INC using a string variable
by CountZero (Bishop) on Sep 23, 2013 at 18:01 UTC
    The following will work:
    unshift @INC, $path;
    Warning: this will only take effect at run-time, so any module loaded with use will not see this as use works at compile time.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      So I can do a require in place of use and it should work correct? Regards, Siddharth
        Yes, that will work. But remember that require is not the same as use: the import sub will not be called

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: ADD directory to @INC using a string variable
by Anonymous Monk on Sep 24, 2013 at 13:30 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055312]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2014-07-12 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls