When installing ActiveState Perl in Windows, unless you have IIS installed, it won't modify the PATHEXT variable (to let you run Perl Scripts without the '.pl' extension).

After doing it by hand for a few years (Control Panel > System > Advanced > Environment Variables and edit the System Variable "PATHEXT"), I wrote this script to do it automatically.

Some day I'd like to automate the process of installing all the software I commonly use in Windows, for a new machine (or a VM); if I ever do, this is something I'll run immediately after installing Perl.

#!/usr/bin/perl -w # # Automatically add '.PL' to the Windows PATHEXT variable so one # can invoke Perl scripts *without* typing the .pl extension. # # NOTE -- Explorer needs to be reloaded for the changes to take # effect; hence the call to reload_explorer(). Even so, changes # still only apply to *new* command windows. # # Many thanks to Tye -- he not only provided the code for the # subroutine reload_explorer(), he's also the author of the # Win32::TieRegistry module which this script depends on. ## ############### ## Libraries ## ############### use strict; use warnings; use File::Basename; use Win32::API; use Win32::TieRegistry( Delimiter => '/' ); ################## ## Main Program ## ################## my $h_sys = $Registry->{"LMachine/SYSTEM"}; my @sets = keys %$h_sys; foreach my $set (@sets) { my $h_set = $h_sys->{$set}; my $h_env = $h_set->{"Control/Session Manager/Environment"}; my $pathext = $h_env->{'PATHEXT'}; if ($pathext || "") { $h_env->{'PATHEXT'} = add_pathext($pathext, '.PL'); print "[$set]\n"; print "Before: $pathext\n"; print " After: $h_env->{PATHEXT}\n\n"; } } reload_explorer(); ################# ## Subroutines ## ################# sub add_pathext { my ($pathext, $ext) = @_; my $a_paths = [ split(';', $pathext) ]; my $b_exists = 0; my $a_modify = [ ]; foreach my $path (@$a_paths) { ($path eq $ext) and $b_exists = 1; push @$a_modify, $path; } $b_exists or push @$a_modify, $ext; return join ';', @$a_modify; } sub reload_explorer { use constant HWND_BROADCAST => 0xffff; use constant WM_SETTINGCHANGE => 0x001A; use constant SMTO_ABORTIFHUNG => 2; my $send = Win32::API->new( 'user32', 'SendMessageTimeout', 'LLLPLLL', 'L', ) or die "Can't load SendMessageTimeout(): $^E\n"; $send->Call( HWND_BROADCAST(), WM_SETTINGCHANGE(), 0, "Environment", SMTO_ABORTIFHUNG(), 5000, 0, ); }

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Add .PL extension to PATHEXT automatically by liverpole

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.