Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://737755]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2017-04-26 04:53 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (468 votes). Check out past polls.