I showed my program to a Perl hacker and got a lot of good feedback on it. He didn’t like my tedious table parsing and general reinventing of core Perl features with mediocre code. He suggested I look at Filter::Simple.

I have to say that Active State’s package manager program for CPAN is a pleasure to use. It’s exactly the sort of painless no-hacking type of tool that Windows development practices breed addictions to in its developers. No hacking around in weird script files. No posting questions on forums. Yea! So getting Filter::Simple is not a problem… and dashing out a quick test project is a piece of cake thanks to the good documentation on CPAN:

#!/usr/bin/perl #squig.pm package SQUIG; use Filter::Simple; FILTER { s/(\w+)~(\w+)~(\w+)/try('\1', '\2', '\3')/g; }; 1;

#!/usr/bin/perl #test.pl use SQUIG; sub try { my ($a, $b, $c) = @_; print "$a...$b...$c...

"; } Abba~Babba~Dabba;

I think this easy-to-use tool can solve about 80% of my syntactic sugar issues. The only hitch I see is that I’d like to go beyond a simple sed-like command and call subroutines or functions on the replacement side so I can get different results depending on what turns up in the regexp match. For instance, I’d want to adapt the above “squiggle” filter to put single quotes around text, but leave scalars plain. I might want to transform Foo~Bar~$baz into $self->a_function_call(‘Foo’, ‘Bar’, $baz). (I’m not sure I’d want to see a regex that could do that….) This could help me clean up my object code and eliminate some of the ugliness in the method routines. (Although I may be using Moose in a hamfisted way to begin with….) Turning this technique to my data file parsing of table data… I could create simple functions that build up my ‘environment’ objects. Instead of parsing text and ‘manually’ loading up Moose objects, I could use the sourcefilter transform my table definitions directly into perl code wherever that makes sense.

This also brings me back to my original ‘dream’ project. I could create my own shorthand for a certain class of objects. I could create a set of source filters that get rid of the boiler plate. Instead of hacking up my own evalutator or abusing eval, I could rearrange functions however I like based on the keywords I tag with them.

My spider sense is tingling, though. I suspect this is only marginally less evil than my original inclination. This technique is not covered in any of my Perl books. Larry mentions it in passing in the Camel book, the Perl Cookbook mentions it only in the context of a switch command hack, and Mastering Perl explicitly avoids it. It’s strange to me that Perl literature is so far removed from how I actually use Perl day-to-day. Without word-of-mouth type tips from ‘real’ Perl hackers, I would never have thought to try either Moose or source filters… but that’s what I’m leaning on the most in my architecture. Is there really a gap or do I want bizarre things from my programming environment?

Share this: Twitter

Facebook

Like this: Like Loading... Related