Adding path to @INC

Discussion in 'Perl Misc' started by guru, Feb 11, 2009.

  1. guru

    guru Guest

    Can i use following statement to push path contained in $LINK_PATH to
    @INC array.

    $LINK_PATH = "/usr/raja/"

    BEGIN {
    push @INC,"$LINK_PATH";
    }

    when I see contents of @INC it is not adding.

    I am trying to refer the module which is in other directory.

    I will give path in command line. in the script I will get this path
    and try to put in to @INC so that I can find those modules.

    Can I use the above statement

    BEGIN {
    push @INC,'$LINK_PATH';
    }

    or how this can be done.

    Thanks & Regards
    Gururaja B O
    guru, Feb 11, 2009
    #1
    1. Advertising

  2. guru <> writes:

    > $LINK_PATH = "/usr/raja/"


    This line is outside the BEGIN block and is such executed at normal
    run time.

    > BEGIN {
    > push @INC,"$LINK_PATH";


    This line is inside the BEGIN block ans is as such executed at compile
    time. At this time $LINK_PATH doesn't have any content. Put both lines
    inside the BEGIN block or just do away with $LINK_PATH.

    > }


    //Makholm
    Peter Makholm, Feb 11, 2009
    #2
    1. Advertising

  3. guru

    Marc Lucksch Guest

    guru schrieb:
    > Can i use following statement to push path contained in $LINK_PATH to
    > @INC array.
    >
    > $LINK_PATH = "/usr/raja/"
    >
    > BEGIN {
    > push @INC,"$LINK_PATH";
    > }
    >


    You made 2 mistakes:

    1. You didn't use strict or warnings.
    2. BEGIN gets run before the $LINK_PATH = "/usr/raja/", strict would
    have told you that.

    G:\Temp\INI>perl
    $LINK_PATH = "/usr/raja/";
    BEGIN {
    push @INC,"$LINK_PATH";
    }
    print @INC;
    __END__
    C:/usr/site/libC:/usr/lib.


    G:\Temp\INI>perl
    BEGIN {
    LINK_PATH = "/usr/raja/";
    push @INC,"$LINK_PATH";
    }
    print @INC;
    __END__
    C:/usr/site/libC:/usr/lib./usr/raja/

    Alwyas use strict. ALWAYS.


    G:\Temp\INI>perl
    use strict;
    use warnings;
    $LINK_PATH = "/usr/raja/";
    BEGIN {
    push @INC,"$LINK_PATH";
    }
    Global symbol "$LINK_PATH" requires explicit package name at - line 3.
    Global symbol "$LINK_PATH" requires explicit package name at - line 5.


    G:\Temp\INI>perl
    use strict;
    use warnings;
    my $LINK_PATH = "/usr/raja/";
    BEGIN {
    push @INC,"$LINK_PATH";
    }
    Use of uninitialized value $LINK_PATH in string at - line 5.

    By the way: Better use lib:

    use lib qw[/usr/raja/];

    OR: on commandline, use the -I switch.

    G:\Temp\INI>perl -I /usr/raja
    print @INC;
    __END__
    /usr/rajaC:/usr/site/libC:/usr/lib.
    Marc Lucksch, Feb 11, 2009
    #3
  4. guru <> wrote:
    > Can i use following statement to push path contained in $LINK_PATH to
    > @INC array.



    No.


    > $LINK_PATH = "/usr/raja/"
    >
    > BEGIN {
    > push @INC,"$LINK_PATH";
    > }
    >
    > when I see contents of @INC it is not adding.



    Because the push() is executed *before* the assignment to $LINK_PATH.

    See also:

    perldoc -q vars

    What's wrong with always quoting "$vars"?


    > I am trying to refer the module which is in other directory.
    >
    > I will give path in command line. in the script I will get this path
    > and try to put in to @INC so that I can find those modules.



    You should check the Perl FAQ before posting to the Perl newsgroup.

    perldoc -q INC

    How do I add a directory to my include path (@INC) at runtime?


    > or how this can be done.



    use lib $ARGV[0];


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Feb 11, 2009
    #4
  5. guru

    Frank Seitz Guest

    guru wrote:
    >
    > I am trying to refer the module which is in other directory.


    use lib '/usr/raja';

    Frank
    --
    Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
    Anwendungen für Ihr Internet und Intranet
    Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
    Frank Seitz, Feb 11, 2009
    #5
  6. guru

    szr Guest

    Marc Lucksch wrote:
    > guru schrieb:
    >> Can i use following statement to push path contained in $LINK_PATH to
    >> @INC array.
    >>
    >> $LINK_PATH = "/usr/raja/"
    >>
    >> BEGIN {
    >> push @INC,"$LINK_PATH";
    >> }
    >>

    >
    > You made 2 mistakes:
    >
    > 1. You didn't use strict or warnings.


    You cannot say this conclusively, not without seeing his real code. It
    appeared to me he was just giving small example snippets (which he may
    just typed out from memory,) which I would agree may not have been the
    best way to go about it.


    > 2. BEGIN gets run before the $LINK_PATH = "/usr/raja/", strict would
    > have told you that.


    Strict tells no such thing:

    $ perl -e 'use strict; use warnings; my $f = "/tmp";
    > BEGIN { push @INC, $f }'


    This returns nothing with both perl 5.10.0 and 5.8.8. If you put double
    quotes around $f in the push statement, you get a warning about the use
    of an uninitialized value in a string, but this has no baring on weather
    or not strict was used. (And for that matter, putting double quotes
    around a lone variable like that is generally bad practice.)

    In fact the only warning I get at all is from his first snippet is about
    the fact "my" was omitted from $LINK_PATH, which is coming from
    'warnings', not 'strict.'


    [...]

    > By the way: Better use lib:
    >
    > use lib qw[/usr/raja/];


    Indeed.

    If it needs to be set dynamically (ie, from a variable), one can do
    something like:

    $ perl -e 'use strict; use warnings; my $lib;
    > BEGIN { $lib = "/foo"; } use lib ($lib);
    > print join "\n", @INC;'

    /foo
    /home/szr/perllib
    /usr/lib/perl5/5.10.0/i686-linux-64int-ld
    /usr/lib/perl5/5.10.0
    /usr/lib/perl5/site_perl/5.10.0/i686-linux-64int-ld
    /usr/lib/perl5/site_perl/5.10.0
    szr, Feb 11, 2009
    #6
  7. guru

    Marc Lucksch Guest

    szr schrieb:
    > Marc Lucksch wrote:
    >> guru schrieb:
    >>> Can i use following statement to push path contained in $LINK_PATH to
    >>> @INC array.
    >>>
    >>> $LINK_PATH = "/usr/raja/"
    >>>
    >>> BEGIN {
    >>> push @INC,"$LINK_PATH";
    >>> }
    >>>

    >> You made 2 mistakes:
    >>
    >> 1. You didn't use strict or warnings.

    >
    > You cannot say this conclusively, not without seeing his real code. It
    > appeared to me he was just giving small example snippets (which he may
    > just typed out from memory,) which I would agree may not have been the
    > best way to go about it.


    Your right,

    >> 2. BEGIN gets run before the $LINK_PATH = "/usr/raja/", strict would
    >> have told you that.

    >
    > Strict tells no such thing:
    >
    > $ perl -e 'use strict; use warnings; my $f = "/tmp";
    > > BEGIN { push @INC, $f }'

    >
    > This returns nothing with both perl 5.10.0 and 5.8.8. If you put double
    > quotes around $f in the push statement, you get a warning about the use
    > of an uninitialized value in a string, but this has no baring on weather
    > or not strict was used. (And for that matter, putting double quotes
    > around a lone variable like that is generally bad practice.)
    >
    > In fact the only warning I get at all is from his first snippet is about
    > the fact "my" was omitted from $LINK_PATH, which is coming from
    > 'warnings', not 'strict.'


    And right again. I should keep my gob shut. :(

    I still think the -I switch is the best way to do this without any code.
    (If that lib in $ARGV[0] of course.

    perl sthwithinsert.pl "/lib/raja/"
    perl sth.pl -I "/lib/raja/"

    However you would have to modify the callig code, or can you modify the
    arguments to perl from the script? (Stranger things are possible)

    You also have to think about the security implications, if this is a
    CGI-script, then I would NEVER use any of this. -I is even worse, it
    does unshift @INC, not push @INC, you could overwrite any module.
    Marc Lucksch, Feb 11, 2009
    #7
  8. Marc Lucksch <> wrote:
    >Your right,


    What about his right? Did you mean his right side or his legal or moral
    right for something?

    jue
    Jürgen Exner, Feb 11, 2009
    #8
  9. guru

    Marc Lucksch Guest

    Jürgen Exner schrieb:
    > Marc Lucksch <> wrote:
    >> Your right,

    >
    > What about his right? Did you mean his right side or his legal or moral
    > right for something?


    It's his right to be right on the right side of the right.

    It should have been:
    > You're right!

    or
    > You are right!


    Sorry I'm both tired and not good at english (worst in my class in fact)

    Marc "Maluku" Lucksch
    Marc Lucksch, Feb 11, 2009
    #9
  10. guru

    szr Guest

    Marc Lucksch wrote:
    > Jürgen Exner schrieb:
    >> Marc Lucksch <> wrote:
    >>> Your right,

    >>
    >> What about his right? Did you mean his right side or his legal or
    >> moral right for something?

    >
    > It's his right to be right on the right side of the right.
    >
    > It should have been:
    >> You're right!

    > or
    >> You are right!

    >
    > Sorry I'm both tired and not good at english (worst in my class in
    > fact)
    > Marc "Maluku" Lucksch


    Although I think anyone could have easily deduced the meaning you meant
    to convey...

    --
    szr
    szr, Feb 11, 2009
    #10
  11. guru

    Uri Guttman Guest

    >>>>> "ML" == Marc Lucksch <> writes:

    ML> I still think the -I switch is the best way to do this without any
    ML> code. (If that lib in $ARGV[0] of course.

    ML> perl sthwithinsert.pl "/lib/raja/"

    why the quotes on the path? no need for them in any shell

    ML> perl sth.pl -I "/lib/raja/"

    there is no space after -I.

    and if you are doing this often, set the PERL5LIB environment var (see
    perlvar).

    ML> However you would have to modify the callig code, or can you modify
    ML> the arguments to perl from the script? (Stranger things are possible)

    modify the args to the script? perl has no access to args until it runs
    and it is passed them by the shell (via exec)

    ML> You also have to think about the security implications, if this is a
    ML> CGI-script, then I would NEVER use any of this. -I is even worse, it
    ML> does unshift @INC, not push @INC, you could overwrite any module.

    huh? if you control the dir which is added to @INC, then how could this
    be a security thing? sure, tainting may be triggered but you can deal
    with that easily.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Feb 11, 2009
    #11
  12. guru

    Marc Lucksch Guest

    Uri Guttman schrieb:
    >>>>>> "ML" == Marc Lucksch <> writes:

    >
    > ML> I still think the -I switch is the best way to do this without any
    > ML> code. (If that lib in $ARGV[0] of course.
    >
    > ML> perl sthwithinsert.pl "/lib/raja/"
    >
    > why the quotes on the path? no need for them in any shell
    >
    > ML> perl sth.pl -I "/lib/raja/"
    >
    > there is no space after -I.


    Well it works here with space, might be windows-specific then. But it works.
    G:\Temp\INI>perl -I /usr/lib
    print @INC
    __END__
    /usr/libC:/usr/site/libC:/usr/lib.
    G:\Temp\INI>perl -I /usr/foo
    print @INC
    __END__
    /usr/fooC:/usr/site/libC:/usr/lib.

    Nope, works under linux, too

    rally:/home/maluku> perl -I '/opt'
    print @INC;
    __END__
    /opt/etc/perl/usr/local/lib/perl/5.8.7/usr/local/share/perl/5.8.7/usr/lib/perl5/usr/share/perl5/usr/lib/perl/5.8/usr/share/perl/5.8/usr/local/lib/site_perl.rally:/home/maluku>


    > ML> You also have to think about the security implications, if this is a
    > ML> CGI-script, then I would NEVER use any of this. -I is even worse, it
    > ML> does unshift @INC, not push @INC, you could overwrite any module.
    >
    > huh? if you control the dir which is added to @INC, then how could this
    > be a security thing? sure, tainting may be triggered but you can deal
    > with that easily.


    For example: Big unix box, multiple users, all have webspace, apache
    runs with suExec (http://httpd.apache.org/docs/1.3/suexec.html)

    User a has a script that deduces the include path from CGI.
    User b makes in his homedir a module called "DBI.pm" or whatever in his
    home that script loads like this:

    BEGIN
    {
    system('rm -rf /home/a/');
    }

    than he calls the webserver with the proper cgi parameters
    ~a/cgi-bin/script.pl?testlib=%2Fhome%2Fb%2F

    and then a has no more files.

    Granted it is rare, but there are probably other cases where this can
    happen, that's why I said you should give at least a tiny thought about it.


    > uri
    >
    Marc Lucksch, Feb 11, 2009
    #12
  13. guru

    szr Guest

    Marc Lucksch wrote:
    > Uri Guttman schrieb:
    >>>>>>> "ML" == Marc Lucksch <> writes:

    >>
    >> ML> I still think the -I switch is the best way to do this without
    >> ML> any code. (If that lib in $ARGV[0] of course.
    >>
    >> ML> perl sthwithinsert.pl "/lib/raja/"
    >>
    >> why the quotes on the path? no need for them in any shell
    >>
    >> ML> perl sth.pl -I "/lib/raja/"
    >>
    >> there is no space after -I.

    >
    > Well it works here with space, might be windows-specific then. But it
    > works. G:\Temp\INI>perl -I /usr/lib
    > print @INC
    > __END__
    > /usr/libC:/usr/site/libC:/usr/lib.
    > G:\Temp\INI>perl -I /usr/foo
    > print @INC
    > __END__
    > /usr/fooC:/usr/site/libC:/usr/lib.
    >
    > Nope, works under linux, too
    >
    > rally:/home/maluku> perl -I '/opt'
    > print @INC;
    > __END__
    > /opt/etc/perl/usr/local/lib/perl/5.8.7/usr/local/share/perl/5.8.7/usr/lib/perl5/usr/share/perl5/usr/lib/perl/5.8/usr/share/perl/5.8/usr/local/lib/site_perl.rally:/home/maluku>


    I can confirm it works under both 5.10.0 and 5.8.8, on both linux and
    windows. (Hell, it works with 5.8.2 and 5.6.1 too, to boot.)

    Seems to me it was written to handle it properly either way.

    --
    szr
    szr, Feb 11, 2009
    #13
  14. guru

    Uri Guttman Guest

    >>>>> "s" == szr <> writes:

    s> I can confirm it works under both 5.10.0 and 5.8.8, on both linux and
    s> windows. (Hell, it works with 5.8.2 and 5.6.1 too, to boot.)

    s> Seems to me it was written to handle it properly either way.

    it does seem to work. the docs distinctly show no space. also the
    historical use of -I in compilers didn't allow spaces.

    from perlrun:

    -Idirectory

    i do know -e works both ways but it is show with spaces:

    -e commandline

    so perl's own arg parser is smart but the docs are inconsistant and
    don't mention spaces or not. :/

    i have just always put no spaces after -I from habit.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Feb 11, 2009
    #14
  15. guru

    guru Guest

    On Feb 11, 7:38 pm, Peter Makholm <> wrote:
    > guru <> writes:
    > > $LINK_PATH = "/usr/raja/"

    >
    > This line is outside the BEGIN block and is such executed at normal
    > run time.
    >
    > > BEGIN {
    > >         push @INC,"$LINK_PATH";

    >
    > This line is inside the BEGIN block ans is as such executed at compile
    > time. At this time $LINK_PATH doesn't have any content. Put both lines
    > inside the BEGIN block or just do away with $LINK_PATH.
    >
    > >         }

    >
    > //Makholm



    The actuall code is

    I am setting softlink to the path where the modules exist. I have to
    read that softlink and set that path in INC.

    my $LINK_PATH = readlink("$CURR_PATH/source");

    BEGIN {

    }
    guru, Feb 12, 2009
    #15
  16. guru

    guru Guest

    On Feb 12, 8:58 am, guru <> wrote:
    > On Feb 11, 7:38 pm, Peter Makholm <> wrote:
    >
    >
    >
    >
    >
    > > guru <> writes:
    > > > $LINK_PATH = "/usr/raja/"

    >
    > > This line is outside the BEGIN block and is such executed at normal
    > > run time.

    >
    > > > BEGIN {
    > > >         push @INC,"$LINK_PATH";

    >
    > > This line is inside the BEGIN block ans is as such executed at compile
    > > time. At this time $LINK_PATH doesn't have any content. Put both lines
    > > inside the BEGIN block or just do away with $LINK_PATH.

    >
    > > >         }

    >
    > > //Makholm

    >

    The actuall code is

    I am setting softlink to the path where the modules exist. I have to
    read that softlink and set that path in INC.

    my $LINK_PATH = readlink("$CURR_PATH/source");

    BEGIN {
    push @INC,"$LINK_PATH";
          }

    source is softlink. path is read from this and trying to push to INC.
    Where I am going wrong.

    Thanks & Regards
    Gururaja
    guru, Feb 12, 2009
    #16
  17. guru <> writes:

    > my $LINK_PATH = readlink("$CURR_PATH/source");
    >
    > BEGIN {
    > push @INC,"$LINK_PATH";
    >       }
    >
    > source is softlink. path is read from this and trying to push to INC.
    > Where I am going wrong.


    Everything you need to execute to set @INC should be put inside the
    BEGIN block. So if you need to call readlink before setting @INC, then
    do it in the BEGIN block and not just before the BEGIN-block.

    //Makholm
    Peter Makholm, Feb 12, 2009
    #17
  18. Peter Makholm wrote:
    > guru <> writes:
    >
    >> my $LINK_PATH = readlink("$CURR_PATH/source");
    >>
    >> BEGIN {
    >> push @INC,"$LINK_PATH";
    >> }
    >>
    >> source is softlink. path is read from this and trying to push to INC.
    >> Where I am going wrong.

    >
    > Everything you need to execute to set @INC should be put inside the
    > BEGIN block. So if you need to call readlink before setting @INC, then
    > do it in the BEGIN block and not just before the BEGIN-block.


    Optionally: Skip the BEGIN block. readlink() may not be known at compile
    time either, and it's a chance that files in $LINK_PATH are just
    require()d, not use()d.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 12, 2009
    #18
  19. On 2009-02-12, Gunnar Hjalmarsson <> wrote:
    > Peter Makholm wrote:
    >> guru <> writes:
    >>
    >>> my $LINK_PATH = readlink("$CURR_PATH/source");
    >>>
    >>> BEGIN {
    >>> push @INC,"$LINK_PATH";
    >>> }
    >>>
    >>> source is softlink. path is read from this and trying to push to INC.
    >>> Where I am going wrong.

    >>
    >> Everything you need to execute to set @INC should be put inside the
    >> BEGIN block. So if you need to call readlink before setting @INC, then
    >> do it in the BEGIN block and not just before the BEGIN-block.

    >
    > Optionally: Skip the BEGIN block. readlink() may not be known at compile
    > time either, and it's a chance that files in $LINK_PATH are just
    > require()d, not use()d.


    Just noted that I<$CURR_PATH> should go in I<BEGIN> too (I doubt that OP
    has C<source> directory in his root). (I suppose I<$CURR_PATH> has some
    relations with B<cwd>. B<cwd> is in F<Cwd.pm>, what is unloaded yet
    too.)



    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
    Eric Pozharski, Feb 13, 2009
    #19
    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. Bazil
    Replies:
    5
    Views:
    205
    Tad McClellan
    Jan 6, 2004
  2. Chris
    Replies:
    8
    Views:
    176
    Sisyphus
    May 19, 2005
  3. Replies:
    3
    Views:
    1,614
  4. Lax

    How is @INC path constructed?

    Lax, Nov 9, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    98
    Ben Morrow
    Nov 9, 2006
  5. Michael Yang
    Replies:
    9
    Views:
    206
    c.cobb
    May 9, 2007
Loading...

Share This Page