Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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 ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-24 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found