Perl Modules: Win32

  • Win32::* modules allow direct interaction with Windows elements and the OS.
  • When we renamed the server "Advisor" to "Webster", all my shortcuts were broken.
  • The following program changed all parts of a shortcut that reference \\Advisor and change them to \\Webster
    #!perl -w
    use strict;
    use Win32::Shortcut;
    for my $filename ( glob( @ARGV ) ) {
        my $shortcut = Win32::Shortcut->new( $filename ) or die;
        for my $field ( qw( IconLocation ShortPath Path ) ) {
            $shortcut->{ $field } =~ s[^\\\\advisor\\][\\\\Webster\\]i;
            } # for
        $shortcut->Save( "$filename" );
        warn "$filename\n";

  • When we installed MS Office locally, I had some leftover incorrect data lying around in the registry. Win32::Registry module to the rescue!
    #!perl -w
    use strict;
    use Win32::Registry;
    my $search = "K:\\MSOFFICE\\OFFICE\\";
    my $replace = "C:\\Program Files\\Microsoft Office\\Office\\";
    #&ProcessKey( "SOFTWARE\\Microsoft\\Office\\8.0" );
    &ProcessKey( "" );
    sub ProcessKey($) {
    	my $keystr = shift;
    	my $curr;
    	#$main::HKEY_LOCAL_MACHINE->Open( $keystr, $curr ) ||
    	$main::HKEY_CLASSES_ROOT->Open( $keystr, $curr ) ||
    		die "Opening [$keystr]: $!";
    	my %vals;
    	$curr->GetValues( \%vals );
    	for my $i ( keys %vals ) {
    		my $key = $vals{ $i };
    		next unless $$key[1] eq REG_SZ;
    		my $valname = $$key[0];
    		my $valdata = $$key[2];
    		next unless $valdata =~ s/^\Q$search\E/$replace/io;
    		$curr->SetValue( $valname, REG_SZ, $valdata );
    		print "$keystr\n";
    		print "  $valname -> $valdata\n";
    	my @keys;
    	$curr->GetKeys( \@keys );
    	for my $i ( @keys ) {
    		ProcessKey( "$keystr\\$i" );
  • Index
    What Is Perl?
    Perl Resources
    Running a Perl Program
    Perl Thinking
    Data Types
    Strings: Single Quoted
    Strings: Double Quoted
    Scalar operations
    Scalar comparisons
    Using Lists
    Control Structures
    Hash Manipulation
    File Handling
    Regex Matching
    Regex Matching: Ex. 1
    Regex Matching: Ex. 2
    Regex Replacing
    Anonymous subs
    Modules: File::Find
    Modules: Internet
    Modules: Win32::*
    Everything Else
      Next page >>>