Cases like your merge/unmerge are really what a macro system is for. Failing that (which Perl does, up until Perl 6), you can use either eval (in simple cases) or a templating engine (in not so simple cases) to generate code for similar things from a common source and still do it Once And Only Once.