Hello Monks of the Monastery,

It's time for the latest installation of bennymack creations.

Today's creation is difficult to name so I'll just try to explain what it does. The problem it attempts to solve is when you have two subs that are slightly different but mostly the same. The difference between the two subs might be just one line.

So, you could copy and paste the sub into two separate subs then maintain them both and just work around the differences. Or you can come up with a way to have both subs live together and programmatically, in as hygenic a manner as possible, alter them as needed. Time for some example code.

################# UseEvalAttributes2.pm package UseEvalAttributes2; use strict; use warnings; use EvalAttributes( qw[CONDITION NAME] ); BEGIN { our @ISA = qw[EvalAttributes]; } INIT { *sub1 = macro_sub( CONDITION => 0, NAME => 'sub1' ); *sub2 = macro_sub( CONDITION => 1, NAME => 'sub2' ); } sub macro_sub : Macro( qw[CONDITION NAME] ) { print NAME . " COMMON CODE\n"; print NAME . " CONDITIONAL CODE\n" if CONDITION; return; } 1; __END__ $ perl -Mstrict -Mwarnings -MB::Deparse -e ' use UseEvalAttributes2; UseEvalAttributes2::sub1(); UseEvalAttributes2::sub2(); print "sub ", B::Deparse->new()->coderef2text( \&UseEvalAttributes2::s +ub1 ), "\n"; print "sub ", B::Deparse->new()->coderef2text( \&UseEvalAttributes2::s +ub2 ), "\n"; ' sub1 COMMON CODE sub2 COMMON CODE sub2 CONDITIONAL CODE sub { package EvalAttributes; BEGIN {${^WARNING_BITS} = "UUUUUUUUUUUU"} use strict 'refs'; local *__ANON__ = 'EvalAttributes::Macro::new_referent'; package UseEvalAttributes2; print "sub1 COMMON CODE\n"; '???'; return; } sub { package EvalAttributes; BEGIN {${^WARNING_BITS} = "UUUUUUUUUUUU"} use strict 'refs'; local *__ANON__ = 'EvalAttributes::Macro::new_referent'; package UseEvalAttributes2; print "sub2 COMMON CODE\n"; print "sub2 CONDITIONAL CODE\n"; return; }

I hope the results are clear and more or less speak for themselves. I'm leaving out implementation details for now as they're not overly pretty. I like this approach over other subref/closure/variable/conditional approaches because it compiles only the code you absolutely need so there's no execution overhead. It's all determined when the subs are manufactured.

My questions are, as usual, is it worthwhile? What would be a better way to do it? Can I leverage an existing CPAN module for this purpose? OR am I just an idiot...


In reply to Data driven programming? Not sure. by bennymack

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.