Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How do you modify the Win32 PATH statement in a Perl script?

by MoonShadow (Initiate)
on Jan 20, 2009 at 15:56 UTC ( [id://737605]=perlquestion: print w/replies, xml ) Need Help??

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

I have been all over here and Google searching for this answer and I have yet to come across something substantial that addresses this piece.

What I need to do is have my perl script add/modify the windows path statement to include a few directories that are required to run programs ONLY during the execution of the script.

I.E.: Program 123.exe is located in C:\123\ and program 456.exe is located in C:\Program Files\456\. I do not want the path for either 123.exe and 456.exe to be globally available.

I am running Strawberry Perl 5.10.0 for Win32.

Replies are listed 'Best First'.
Re: How do you modify the Win32 PATH statement in a Perl script?
by ikegami (Patriarch) on Jan 20, 2009 at 16:01 UTC

    Use %ENV, or more specifically, $ENV{PATH}.

    $ENV{PATH} = join ';', 'C:\\123', '"C:\\Program Files\\456"', $ENV{PATH};

    or

    $ENV{PATH} = qq{C:\\123;"C:\\Program Files\\456";$ENV{PATH}};

    This changes the PATH for the current process. Children later launched by this process inherit this modified PATH (unless measures are taken to prevent this).

    Update: Improved example.

      Domo arigato gozaimashita Ikegami-san!
Re: How do you modify the Win32 PATH statement in a Perl script?
by moritz (Cardinal) on Jan 20, 2009 at 15:59 UTC
    Have you tried to append the directories to $ENV{PATH}?

    See also %ENV.

Re: How do you modify the Win32 PATH statement in a Perl script?
by Intrepid (Deacon) on Jan 21, 2009 at 06:54 UTC

    I like to clean up the PATH as well when faced with such an operation (where I am preparing to run external programs and may be adding or removing pathels [path-list elements]).

    Here's a little code to prepare the PATH so that it is less likely to cause problems.

    #!perl use Memoize; use Win32; use strict; sub sp { my $pn; die unless @_ and (($pn)=shift); warn "No such directory: \"$pn\"" && return undef unless -d $pn; ($pn = Win32::GetShortPathName($pn)) =~ s{\\}{/}g; substr ($pn, 1+rindex($pn,'/')) = lc substr ($pn, 1+rindex($pn,'/' +)); $pn } sub smorp { my ($piece) = @_; $piece = lc $piece eq 'system32' ? 'system32' : uc $piece eq 'WINDOWS' ? 'WINDOWS' : $piece; return "/".$piece; } memoize('sp'); memoize('smorp'); BEGIN { use Env qw/@PATH/; use vars ('@thePATH', '%Uniqueness'); } @thePATH = @PATH; @PATH = (); PONGO: for my $ele (@thePATH) { my $npiece; my @shattered = grep {$_} split /(?:[\\\/]+|:)+/ , $ele; my $backtog = index ($ele, ":") == 1 ? # is it <D>: ? uc(shift @shattered).":" : $ele =~ m#[\\/]{2}# ? # or is it UNC ? "//".shift (@shattered) : do { die "UNRECOGNIZED PATHEL FORMAT" }; BLA: { $backtog .= smorp ($npiece) while @shattered && index (($npiece = shift @shattered)," ") == -1; unless (@shattered) { $Uniqueness{lc $backtog}++ and print "# DUPLICATE element skipped (\"$backtog\")\n" or print "$backtog\n"; # or add to an array, or whateve +r you need to do. next PONGO } # print "SPACES IN ${backtog}/${npiece}\n"; $backtog = sp("${backtog}/${npiece}"); redo BLA; } die "Reached code branch we should never see!"; }
Re: How do you modify the Win32 PATH statement in a Perl script?
by imrags (Monk) on Jan 21, 2009 at 09:10 UTC
    you can add your path to the 'lib' within the script
    This will ensure that the path is not available outside the script & you can use whatever modules/directories there
    You can also run the command 'set' on windows command line and grep for what directory you want (using perl)
    Type `set /?` to know more about it...
    use lib 'E:\Modules'; #OR `set $cmd`
    Raghu

Log In?
Username:
Password:

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

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

    No recent polls found