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

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

by Intrepid (Deacon)
on Jan 21, 2009 at 06:54 UTC ( #737755=note: print w/ replies, xml ) Need Help??


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

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!"; }


Comment on Re: How do you modify the Win32 PATH statement in a Perl script?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://737755]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-10-01 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (1 votes), past polls