[ABE.pm] metaprogramming is way cool

Ricardo SIGNES perl.abe at rjbs.manxome.org
Mon May 19 08:38:00 PDT 2008


* "Faber J. Fedor" <faber at linuxnj.com> [2008-05-19T10:52:35]
> On 18/05/08 23:22 -0400, Ricardo SIGNES wrote:
> > Any serious programmer would be really well served to look into this topic,
> > either by learning more Lisp, playing with Moose (and its metaclasses, not
> > just Moose itself) and Class::MOP, or just by thinking about how his next
> > common problem might be made easier not with more code to combat the
> > problem, but with mode code to change the language to make the problem
> > vanish.
> 
> Sounds good, but do you have a practical, _easily understood_ example that
> doesn't require learning a whole new paradigm, complicated program, or
> deep module to get the idea across?

The true value of programming the language is hard to convey trivially.  It's a
paradigm shift, so it basically will require, well, shifting your entire
perspective.

I'm not sure what the easiest route into this house of mirrors is.  A good
start is just to have this question forever in the back of your mind: "What if
instead of adding a new feature, I could make this feature act differently?"

This question applies to things other than programming languages!  When you use
your text editor, you can set options to change the way syntax highlighting
works, or indentation, or a number of other features.  For example, in Vim,
'dip' deletes the current paragraph, determined by some incredibly bizarre
options (see :help paragraph).  The idea is that you're saying d (delete) ip
(paragraph you're inside).  If you're writing something where a paragraph can't
be defined by the options, you might write a macro to replace 'dip' by doing
something more complex.  Of course, if you need to do things other than delete
that paragraph, you'll need to write a macro for each one of those.

What if instead you could write some code -- not set an option -- to redefine
what a paragraph is.  Now everything that expects to work on a "paragraph"
still works, but now it does what you want.  (Unfortunately, you can't do this
in Vim.  You can in Emacs, but ... well, then you're using Emacs!)

A lot of this is about saying, "I don't want to keep adding options to my
routine.  I want to change the meaning of the routine when called in some
contexts."

So, the simple way in, beyond the constant consideration of the question, is to
do more subclassing.  Think about lots of flavors of a routine instead of lots
of options to a routine.

And, c'mon.  Learn Lisp.  It'll make you even more popular with the ladies!

-- 
rjbs


More information about the ABE-pm mailing list