Here's the final code. I implemented Zaxo's suggestion.

Thanks to all who helped. Hope you find this code useful!

#!/usr/bin/perl -w =pod This script adds paths into the system path. Simply change @newpaths to reflect the directories you'd like to add. Author: P. Rivera Portions inspired by: Marza/Perlmonks, node 156805; Zaxo and various monks, node 190779 =cut use strict; my $Registry; my $newpath; my @newpaths=('P:\Scripts\Admin','P:\Scripts\Anly','P:\Perl\bin'); use Win32::TieRegistry ( TiedRef=>\$Registry, Delimiter=>"/", ArrayVal +ues=>1 ); my $env= $Registry->{"LMachine/System/CurrentControlSet/Control/". "Session Manager/Environment/"} or die "Can't open CCS/SM/Env: $^E\n"; my $path= $env->{"/PATH"} or die "Can't get PATH: $^E\n"; print "Path currently is $path->[0]\n\n"; print "-" x 65 . "\n"; for $newpath (@newpaths) { print "Attempting to prepend $newpath to path.\n\n"; #See if new path is the system PATH if ($path->[0] =~ /\Q$newpath\E/ ) { print "$newpath is already in your system PATH. Not ad +ded again.\n"; } else { $path->[0]= $newpath . ";$path->[0]"; print "Path is now $path->[0]\n\n"; $env->{"/PATH"}= $path or print "Can't set PATH!!: $^E +\n"; } print "-" x 65 . "\n"; }

In reply to Re: Trying to determine if path exists in environmental variable by slojuggler
in thread Trying to determine if path exists in environmental variable by slojuggler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.