Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Add .PL extension to PATHEXT automatically

by liverpole (Monsignor)
on Dec 30, 2011 at 23:19 UTC ( #945699=CUFP: print w/ replies, xml ) Need Help??

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$..$/

Comment on Add .PL extension to PATHEXT automatically
Download Code
Re: Add .PL extension to PATHEXT automatically
by Anonymous Monk on Dec 30, 2011 at 23:39 UTC
    sub create_file_association { # Don't leave a space between 'Perl' and '>nul'. # See http://bugs.activestate.com/show_bug.cgi?id=68656 system("assoc .pl=Perl>nul") == 0 or warn "Could not create .pl file type association\n"; system(qq(ftype Perl="$perl" "%1" %* >nul)) == 0 or warn "Could not define command to run .pl files\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://945699]
Front-paged by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-10-21 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (95 votes), past polls