Perl: The Lazy Way to Write WPF

I hate writing boilerplate. Recently I was writing a test tool where I needed to be able to build messages from a WCF interface. That’s a lot of ViewModels and a lot of views and a lot of tedious typing.

That is, of course, unless it’s Friday lunchtime and you happen to have spent the first half of your career working on *nix systems. Enter Cygwin and some now rather sketchy memories of how to write Perl.

$ perl -ne \
'if($next) \
{ \
$_=~/^\s+void\s+(\w+)\s*(([^\)]+)\)/; \
$cType=$1; \
print "public class ${cType}Model:DependencyObject\n{\n"; \
@props=split /,\s+/,$2; \
foreach $p (@props) \
{ \
($type,$name)=split /\s+/,$p; \
$name=~s/^([a-z])/\U$1/; \
print "public static DependencyProperty ${name}Property = DependencyProperty.Register(\"${name}\",typeof($type),typeof(${cType}Model));\npublic $type $name\n{\nget => ($type)GetValue(${name}Property);\nset => SetValue(${name}Property, value);\n}\n"; \
} \
print "}\n"; } ; \
$next = /\[OperationContract\]/ ;' \
< IClient.cs >../../../../../Models.cs

I then just used ReSharper to move the classes into their own separate files.

Yes, I realise that the little snippet of Perl is very poorly written, both from the point of view of its fragility in processing C# and also because these days I really only use Perl for one time lash-ups like this. It’s be 15 years since I could say that I wrote Perl in any professional sense and I’ve forgotten a lot in that time.

My point however isn’t to provide a shining example of Perl for you to cut and paste. It’s to point out that a few lines of text processing script in whatever language, written in a few minutes, can save you from a whole load of tedious typing.

I use Cygwin a lot in programming, because utilities like find, xargs, grep, sed, awk, cut, uniq and bash scripting itself can save a heck of a lot of time.

If you’re not an old *nix wonk like me however all is not lost. Perl can do pretty much everything anyway and there are plenty of Perl implementations for Windows.

Of course there is the world of Powershell too and that’s where I have a confession to make. Aside from learning the basics; just enough to do what I need to do, I haven’t really delved into Powershell. I’m sure Microsoft have put a lot of research into it, but to me it feels really awkward, like you’re always having to jump through hoops to get even the simplest thing done.
I realise the potential hypocrisy of this, Despite being notoriously counter-intuitive, vi has become second nature to me. It’s only when I try to explain to others that I remember that if you’re not thinking about trying to operate an editor over a 300 baud serial link then none of it makes any sense at all.

Anyway, I digress, the simple conclusion is that if you’ve got a mountain of boiler-plate to write, have a think about using some sort of script. There’s a lot of power in your fingertips.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.