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

Dear Monks, Please help me in this simple task.i want to set the environment in which my perl programme should run through the perl programme itself. actually I type on the unix commond prompt setenv PERL5LIB /tmp_mnt/usr4/mrt/vinayPandey/lib/perl5/ which works...i.e. connectts the pgplot module etc.. How to do the same using the programme itself... thanks, VNPandey

Replies are listed 'Best First'.
Re: Setting environment!!
by ZZamboni (Curate) on Jul 03, 2000 at 22:05 UTC
    If what you want is to add directories to Perl's search path, you can use the use lib pragma:
    use lib qw(/your/dir1 /your/dir2);
    You could also manually insert the directory into @INC:
    push @INC, qw(/your/dir1 /your/dir2);
    I believe these two approaches are functionally identical, but someone may correct me about that.

    --ZZamboni

      Pretty close. The lib pragma will muck with @INC at compile time, but pushing like that isn't until runtime. For these two to be basically functionally identical, you would want to wrap the push in a BEGIN block:

      <CODE> BEGIN { push @INC, '/your/path'; } <CODE>

      Cheers,
      KM

Re: Setting environment!!
by KM (Priest) on Jul 03, 2000 at 22:12 UTC
    If you are wanting to add a directory to the include paths for modules you can:

    use lib qw(/your/path); use YourModule;

    The lib pragma will basically add that directory to @INC so you can include modules from that directory. If you want to set that env variable from within the script you can:

    BEGIN { $ENV{PERL5LIB} = "/foo"; }

    Cheers,
    KM

Re: Setting environment!!
by i43s (Novice) on Jul 03, 2000 at 22:25 UTC
    No one ( accept KM, but I think too much information sucks for newbies, so this node answers the question only )has addressed the fact that you want to set the environment variable... give the man what he wants!
    $ENV{PERL5LIB} = '/foo';
    Notice that I didn't do this within BEGIN because you may also want to include other things like CGI.pm and that will break if you set the environment BEFORE you include your packages.

    i_had_a_little_trouble_so_i_came_up_with_this

      If you truly wanted to give the man what he wants you would have done it in BEGIN because he currently sets the environment variable before his program runs, so to mimic this behavior, you would want to set it as soon as possible.