December 19, 2010
Joy, from Perl
I've recently started using Perl for text wrangling instead of PHP -- it started
after much of my PHP included a lot of preg_replace and preg_match calls, which
got me wondering...
There's a certain satisfaction I get from perl that I don't really get from
other languages. There's something about the density that makes it quite
readable, at least for the author. Here's a script I wrote to automatically
add a new build configuration, based on an existing build configuration, with
some modifications, to a given .dsp (VC6 project file):
#!/usr/bin/perl
$sp = "Win32 Release";
$np = "Win32 Nitpicker";
$spd="Release";
$npd="Nitpicker";
$spdir="/Release/";
$npdir="/Nitpicker/";
$libfile = "../../nitpicker/libcmt_nitpick.lib";
while (<>)
{
chomp;
s/\r$//;
s/\n$//;
print $_ . "\r\n";
if (/^!ELSEIF .*$sp".*$/ || /^!IF .*$sp".*$/)
{
s/^!IF/!ELSEIF/;
s/$sp/$np/;
@nc = ($_);
while (<>)
{
chomp;
s/\r$//;
s/\n$//;
if (/^!/)
{
my $tmp=$_;
foreach (@nc)
{
s/$spd/$npd/g;
s/$spdir/$npdir/g;
if (/^# ADD BASE CPP/)
{
s/\/Z\S *//;
s/\/YX *//;
s/\/FR *//;
s/\/FD *//;
s/$/ \/FR \/FD \/Zi/;
}
if (/^# ADD CPP/)
{
s/\/Z\S *//;
s/\/M\S *//;
s/\/O\S *//;
s/\/FR *//;
s/\/FD *//;
s/$/ \/FR \/FD \/Zi \/MT \/Ot \/Og \/D "DEBUG_TIGHT_ALLOC"/;
}
if (/^# ADD LINK32/)
{
s/^# ADD LINK32 /# ADD LINK32 $libfile /;
s/\/debug *//;
s/\/out:/\/nodefaultlib:"LIBCMT" \/out:/;
s/$/ \/debug/;
}
if (/^# ADD BASE LINK32/)
{
s/\/debug *//;
s/\/map *//;
s/\/dll /\/dll \/debug /;
s/$/ \/fixed:no/;
}
print $_ . "\r\n";
}
print $tmp;
last;
}
print $_ . "\r\n";
push(@nc,$_);
}
}
if (/^!MESSAGE.*$sp".*$/ || /^# Name ".*$sp" *$/)
{
s/$sp/$np/;
print $_ . "\r\n";
}
}
In case anybody is interested, Nitpicker is something we've been working on that is similar to Electric Fence or other memory debuggers, but better suited for our workflow/design/etc. We'll probably GPL it once it matures.
6 Comments