Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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$..$/

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found