Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
How many of you have written code that looks something like:
package Some::Class; use strict; use warnings; sub new { my $class = shift; return bless { @_ }, $class; } sub foo { my $self = shift; $self->{foo} = shift if @_; return $self->{foo}; } sub bar { my $self = shift; $self->{bar} = shift if @_; return $self->{bar}; } sub baz { my $self = shift; $self->{baz} = shift if @_; return $self->{baz}; } sub do_something_useful { ... }
Come on, raise your hands. I know I've done this at least a hundred times. Then, I learned about closures and went back and rewrote that code to look something like:
package Some::Class; use strict; use warnings; sub new { my $class = shift; return bless { @_ }, $class; } foreach my $name ( qw( foo bar baz ) ) { no strict 'refs'; *{ __PACKAGE__ . "::$name" } = sub { my $self = shift; $self->{$name} = shift if @_; return $self->{$name}; }; } sub do_something_useful { ... }
Now, instead of 98% of the Perl community being able to maintain my code, I'm down to 0.98%. Several managers I've worked for had made me take out code like that, and for good reason. Just because they hired a Perl expert to write the code doesn't mean that they'll be able to hire someone like that to maintain the code. So, it's back to repetition, right?

<Trumpets sound in the distance /> Module::Compile::TT to the rescue! That code using typeglobs and closures now looks like:

package Some::Class; use strict; use warnings; sub new { my $class = shift; return bless { @_ }, $class; } use tt; [% FOREACH name IN [ 'foo', 'bar', 'baz' ] %] sub [% name %] { my $self = shift; $self->{[% name %]} = shift if @_; return $self->{[% name %]}; } [% END %] no tt; sub do_something_useful { ... }
Whoa! That actually looks readable! Everyone knows how to read TT directives (or they're close enough to your favorite templating module as to be no difference).

But, isn't this a source filter? Well, technically, it is. But, there's a major difference between this and Filter::Simple. Module::Compile::TT compiles this once and installs a .pmc file that you can look at and edit. Or, you could just run TT against this module and see what would happen.

Contrast that to Filter::Simple that won't generates potentially anything and you have no (sane) way of finding out what happened.

The real dealbreaker for me is that I feel pretty sure I could take this to any manager I use to work for and they would all be comfortable with that kind of code in their production codebases. This is code that can be maintained by the masses.


In reply to Module::Compile::TT by dragonchild

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (11)
    As of 2014-12-22 12:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (116 votes), past polls