perlmeditation
leriksen
There is a wisdom that states "If you want to be a great coder, read great code" - in other words, learn from those who have come before. In perl, this can be done quite easily by rummaging around in the catacombs of the Monastery, or by skulking around inside CPAN.
<p>When you start doing this, I think one of the most striking thing is just how many WTDI (Ways To Do It - from TMTOWTDI) some modules employ. Some of my favourites are<p>
<ul>
<li>[cpan://Net::LDAP::Constant] parses its own POD to set the constants it provides - key code is roughly<code>while (<DATA>) { # get a line of POD
if (/^=item\s+(LDAP_\S+)\s+\((.*)\)/) {# look for a POD line of =item LDAP_<constant> (<value>)
$const{$name} = sub () { $value }; # set the constant from what was in the POD !!!
}
}</code><br>Talk about keeping the code and docs in sync - the code IS the doco</li>
<li>When running 'make test' for [cpan://Devel::Cover], it eval's running its own test harness, captures the output, compares it to a set of 'golden' files, which contain its own test output from someone elses machine, for every recent release of perl, to see if your perl works the same as theirs - if so, you pass !!</li>
<li>[cpan://Test::MockObject] (my new lust module - <code>use Test::MockObject qw(schwing);)</code> <br>The method fake_module() contains the most crystal clear example of populating @INC and the symbol table I have ever seen - just beautiful - key code is roughly <code>sub fake_module
{
my ($class, $modname, %subs) = @_; # replace methods in $modname with those in %subs
$INC{ $modname . '.pm' } = 1; # pretend we have already loaded the module
foreach my $sub (keys %subs) { # for each implementation provided ...
*{ $module . '::' . $sub } = $subs{ $sub }; # store the provided implementation in the symbol table
}
}</code></li>
</ul>
<p>Those are just three recent ones where I suddenly stopped reading, sat up and went 'Thats ... incredible'.
<p>Perhaps other monks would care to share the wonderful ideas (and weird ones too ... ) held in CPAN.
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-189168">
<p><i>use brain;</i>
</div></div>