Wednesday, November 14, 2007

Playing Perl Golf

To keep my mind off (extremely miffed), I played perl golf ...

Good boy version (889 chars):

use strict;
use Win32::TieRegistry;
my $Machine = 'localhost'; # make sure the RemoteRegistry services is up
# and the script is executed with user within
# administrator group.
my $Register = 'HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows'.
'/CurrentVersion/Uninstall';
$Register = "//$Machine/$Register";

$Registry->Delimiter("/");
my $remoteKey = $Registry->{$Register};
if (defined $remoteKey) {
foreach my $appKey ( $remoteKey->SubKeyNames ) {
my $DisplayName = $Registry->{"$Register/$appKey/DisplayName"};
my $UninstallString = $Registry->{"$Register/$appKey/UninstallString"};
if (defined $UninstallString && defined $DisplayName) {
$app{$DisplayName} = $UninstallString;
}
}
}
open LOG, ">$0.txt";
foreach my $appName (sort keys %app) {
print LOG "$appName, $app{$appName}\n";
}
close LOG;

Perl golf version (388 chars):

use Win32::TieRegistry;
$R = '//local/HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft'.
'/Windows/CurrentVersion/Uninstall';
$Registry->Delimiter("/");
my %app = map {
$Registry->{"$R/$_/DisplayName"} => $Registry->{"$R/$_/UninstallString"}
} $Registry->{$R}->SubKeyNames;
open LOG, ">$0.txt";
map { print LOG "$_, $app{$_}\n" if defined $_ && $app{$_} } sort keys %app;
close LOG;

No comments: