Can not add a scalar variable of path into @INC?

Discussion in 'Perl Misc' started by Michael Yang, May 9, 2007.

  1. Michael Yang

    Michael Yang Guest

    I need to add the my lib path into @INC:
    When I give the absolute path to the @INC, it works fine:
    use lib "/home/michael/mylib";
    But it doesn't work when I passed mylib as a scalar variable:
    my $libpath = "/home/michael/mylib";
    use lib $libpath;

    In this way, the script can't locate the package in "mylib" path.
    I need to use this scalar variable instead of hard-coded absolute
    path.
    Is there any ways to do this?
    Thanks!
    Michael Yang, May 9, 2007
    #1
    1. Advertising

  2. Michael Yang

    Michael Yang Guest

    On May 9, 10:52 am, Michael Yang <> wrote:
    > I need to add the my lib path into @INC:
    > When I give the absolute path to the @INC, it works fine:
    > use lib "/home/michael/mylib";
    > But it doesn't work when I passed mylib as a scalar variable:
    > my $libpath = "/home/michael/mylib";
    > use lib $libpath;
    >
    > In this way, the script can't locate the package in "mylib" path.
    > I need to use this scalar variable instead of hard-coded absolute
    > path.
    > Is there any ways to do this?
    > Thanks!


    Seems that it doesn't work either using absolute path name.
    During execution, it can not locate the *.pm file under "mylib" path.
    But it works after I set the environment variable of "PERL5LIB" in the
    command line.
    This is not what I really need.
    I want to add the lib path during the script run time.
    Michael Yang, May 9, 2007
    #2
    1. Advertising

  3. Michael Yang <> wrote:

    > But it doesn't work when I passed mylib as a scalar variable:
    > my $libpath = "/home/michael/mylib";



    That statement happens at run time.


    > use lib $libpath;



    That statement happens at compile time.

    compile time comes before run time, so the "use" is evaluated
    before the variable has been given a value.

    Didn't you get an error message like:

    Empty compile time value given to use lib at ... ?


    > I need to use this scalar variable instead of hard-coded absolute
    > path.



    Why do you need to use this scalar variable instead of hard-coded
    absolute path?


    > Is there any ways to do this?



    There is a way to force statements to be evaluated at compile time,
    read up on the BEGIN block in perlmod.pod.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 9, 2007
    #3
  4. Michael Yang

    Michael Yang Guest

    Thanks so much, Tad!
    Here is another problem after trying the BEGIN block statement:
    my $mylib = undef;
    BEGIN
    {
    $mylib = "/home/michael/mylib";
    unshift @INC, $mylib;
    }

    print "\$mylib is $mylib.\n" ###########the value of $mylib is
    empty.

    The statement inside BEGIN is evaluated during compile time, seems not
    in run time.
    The $mylib value is set to undef outside the block.
    How could make the codes inside BEGIN be evaluated both during compile
    time and run time?
    It's really appreciated of your answer!

    On May 9, 11:19 am, Tad McClellan <> wrote:
    > Michael Yang <> wrote:
    > > But it doesn't work when I passed mylib as a scalar variable:
    > > my $libpath = "/home/michael/mylib";

    >
    > That statement happens at run time.
    >
    > > use lib $libpath;

    >
    > That statement happens at compile time.
    >
    > compile time comes before run time, so the "use" is evaluated
    > before the variable has been given a value.
    >
    > Didn't you get an error message like:
    >
    > Empty compile time value given to use lib at ... ?

    Yes, I got this error msg before and didn't get to it.
    >
    > > I need to use this scalar variable instead of hard-coded absolute
    > > path.

    >
    > Why do you need to use this scalar variable instead of hard-coded
    > absolute path?

    To make my toolkit portable to different machines.
    >
    > > Is there any ways to do this?

    >
    > There is a way to force statements to be evaluated at compile time,
    > read up on the BEGIN block in perlmod.pod.
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
    Michael Yang, May 9, 2007
    #4
  5. Michael Yang

    Sisyphus Guest

    "Michael Yang" <> wrote in message
    ..
    ..
    > The $mylib value is set to undef outside the block.


    Why ?

    > How could make the codes inside BEGIN be evaluated both during compile
    > time and run time?


    No need to evaluate the code that's in the BEGIN at runtime - $mylib will
    retain the value it was assigned in the BEGIN{} block:

    --------------------------
    use warnings;

    BEGIN {
    $mylib = '/some/place';
    unshift @INC, $mylib;
    }

    print "@INC\n", $mylib, "\n";
    --------------------------

    For me that produces:

    /some/place C:/perl58_M/lib C:/perl58_M/site/lib .
    /some/place

    On the other hand, if you assign another value to $mylib during runtime (as
    you have done) then, of course, $mylib will contain that new value (which
    was undef in the example you provided).

    Cheers,
    Rob
    Sisyphus, May 9, 2007
    #5
  6. Michael Yang

    Michael Yang Guest

    On May 9, 1:17 pm, "Sisyphus" <> wrote:
    > "Michael Yang" <> wrote in message
    >
    > .
    > .
    >
    > > The $mylib value is set to undef outside the block.

    >
    > Why ?
    >
    > > How could make the codes inside BEGIN be evaluated both during compile
    > > time and run time?

    >
    > No need to evaluate the code that's in the BEGIN at runtime - $mylib will
    > retain the value it was assigned in the BEGIN{} block:
    >
    > --------------------------
    > use warnings;
    >
    > BEGIN {
    > $mylib = '/some/place';
    > unshift @INC, $mylib;
    >
    > }
    >
    > print "@INC\n", $mylib, "\n";
    > --------------------------
    >
    > For me that produces:
    >
    > /some/place C:/perl58_M/lib C:/perl58_M/site/lib .
    > /some/place
    >
    > On the other hand, if you assign another value to $mylib during runtime (as
    > you have done) then, of course, $mylib will contain that new value (which
    > was undef in the example you provided).
    >
    > Cheers,
    > Rob


    Thank you so much, Rob!
    Now I have got it works. I should not assign value to the $mylib, only
    need to declare it.

    Thanks again~
    Michael.
    Michael Yang, May 9, 2007
    #6
  7. Michael Yang

    Michael Yang Guest

    On May 9, 1:17 pm, "Sisyphus" <> wrote:
    > "Michael Yang" <> wrote in message
    >
    > .
    > .
    >
    > > The $mylib value is set to undef outside the block.

    >
    > Why ?
    >
    > > How could make the codes inside BEGIN be evaluated both during compile
    > > time and run time?

    >
    > No need to evaluate the code that's in the BEGIN at runtime - $mylib will
    > retain the value it was assigned in the BEGIN{} block:
    >
    > --------------------------
    > use warnings;
    >
    > BEGIN {
    > $mylib = '/some/place';
    > unshift @INC, $mylib;
    >
    > }
    >
    > print "@INC\n", $mylib, "\n";
    > --------------------------

    use warnings;
    BEGIN {
    $mylib = '/some/place';
    unshift @INC, $mylib;
    }
    print "@INC\n", $mylib, "\n";

    Is the new value of @INC be available to the sub-process of this
    script?
    for example, invoking another Perl script from here. Will the script
    inherit this new @INC values, containing the extra search path of
    $mylib?

    >
    > For me that produces:
    >
    > /some/place C:/perl58_M/lib C:/perl58_M/site/lib .
    > /some/place
    >
    > On the other hand, if you assign another value to $mylib during runtime (as
    > you have done) then, of course, $mylib will contain that new value (which
    > was undef in the example you provided).
    >
    > Cheers,
    > Rob
    Michael Yang, May 9, 2007
    #7
  8. Michael Yang

    Sisyphus Guest

    "Michael Yang" <> wrote in message
    news:...
    ..
    ..
    >
    > Is the new value of @INC be available to the sub-process of this
    > script?
    > for example, invoking another Perl script from here. Will the script
    > inherit this new @INC values, containing the extra search path of
    > $mylib?


    No - you can check as follows:

    --- try.pl ---
    use warnings;

    BEGIN {
    $mylib = '/some/place';
    unshift @INC, $mylib;
    }

    print "@INC\n", $mylib, "\n";

    system("perl try2.pl");
    -----------

    --- try2.pl ---
    use warnings;

    print "@INC\n";
    --------------

    When I run 'perl try.pl' I get:

    /some/place C:/perl58_M/lib C:/perl58_M/site/lib .
    /some/place
    C:/perl58_M/lib C:/perl58_M/site/lib .

    So clearly, the modified @INC is not inherited.

    You need to rewrite try.pl and try2.pl something like:

    --- try.pl ---
    use warnings;

    BEGIN {
    $mylib = '/some/place';
    unshift @INC, $mylib;
    }

    print "@INC\n", $mylib, "\n";

    system("perl try2.pl $mylib");
    -----------

    --- try2.pl ---
    use warnings;

    BEGIN {
    unshift @INC, $ARGV[0];
    }

    print "@INC\n";
    --------------

    Now when I run 'perl try.pl' I get:

    /some/place C:/perl58_M/lib C:/perl58_M/site/lib .
    /some/place
    /some/place C:/perl58_M/lib C:/perl58_M/site/lib .

    (The modified @INC has been successfully passed on to try2.pl.)

    Cheers,
    Rob
    Sisyphus, May 9, 2007
    #8
  9. Michael Yang

    Michael Yang Guest

    On May 9, 3:59 pm, "Sisyphus" <> wrote:
    > "Michael Yang" <> wrote in message
    >
    > news:...
    > .
    > .
    >
    >
    >
    > > Is the new value of @INC be available to the sub-process of this
    > > script?
    > > for example, invoking another Perl script from here. Will the script
    > > inherit this new @INC values, containing the extra search path of
    > > $mylib?

    >
    > No - you can check as follows:
    >
    > --- try.pl ---
    > use warnings;
    >
    > BEGIN {
    > $mylib = '/some/place';
    > unshift @INC, $mylib;
    >
    > }
    >
    > print "@INC\n", $mylib, "\n";
    >
    > system("perl try2.pl");
    > -----------
    >
    > --- try2.pl ---
    > use warnings;
    >
    > print "@INC\n";
    > --------------
    >
    > When I run 'perl try.pl' I get:
    >
    > /some/place C:/perl58_M/lib C:/perl58_M/site/lib .
    > /some/place
    > C:/perl58_M/lib C:/perl58_M/site/lib .
    >
    > So clearly, the modified @INC is not inherited.
    >
    > You need to rewrite try.pl and try2.pl something like:
    >
    > --- try.pl ---
    > use warnings;
    >
    > BEGIN {
    > $mylib = '/some/place';
    > unshift @INC, $mylib;
    >
    > }
    >
    > print "@INC\n", $mylib, "\n";
    >
    > system("perl try2.pl $mylib");
    > -----------
    >
    > --- try2.pl ---
    > use warnings;
    >
    > BEGIN {
    > unshift @INC, $ARGV[0];
    >
    > }
    >
    > print "@INC\n";
    > --------------
    >
    > Now when I run 'perl try.pl' I get:
    >
    > /some/place C:/perl58_M/lib C:/perl58_M/site/lib .
    > /some/place
    > /some/place C:/perl58_M/lib C:/perl58_M/site/lib .
    >
    > (The modified @INC has been successfully passed on to try2.pl.)
    >
    > Cheers,
    > Rob


    Thank you so much for your reply!

    It seems I have to assign the values to $ENV{'PERL5LIB'} , so that it
    can be inherited.
    Michael.
    Michael Yang, May 9, 2007
    #9
  10. Michael Yang

    c.cobb Guest

    On May 8, 10:57 pm, Michael Yang <> wrote
    > > Why do you need to use this scalar variable instead of hard-coded
    > > absolute path?

    >
    > To make my toolkit portable to different machines.


    Here's one way to create relocatable scripts that
    never need any changes to directory paths no matter
    where in the directory tree you move them. This also
    works when running a Perl script across NFS mount
    points, network share drives and/or as a CGI script.
    http://search.cpan.org/search?query=ptools::local
    FYI,
    c.cobb, May 9, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clint Olsen
    Replies:
    6
    Views:
    340
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  2. Bazil
    Replies:
    5
    Views:
    201
    Tad McClellan
    Jan 6, 2004
  3. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    153
    Arndt Jonasson
    Jan 27, 2005
  4. Replies:
    3
    Views:
    1,569
  5. PerlFAQ Server
    Replies:
    0
    Views:
    113
    PerlFAQ Server
    Mar 2, 2011
Loading...

Share This Page