poem
benn
Wondering about the whole human condition recently, I decided to have a look at some early
source code to see if it could be tightened up in any way, or even spot any glaring bugs. [Ovid] has had a look at [id://49419|Genesis] before (which I think may actually be part of the Vedas:: namespace), but I wanted to check out the main 'init()' routine.
<p>A quick Perl-rewrite-hack of John 1.1 initially produced the code below.
Note - all code is untested - I didn't dare run it until after peer review.
<code>
#!/usr/bin/perl
use strict;
use warnings;
my $word;
BEGIN {
# In the beginning was the Word,
# and the Word was with God, and the Word was God.
$word='God'; # Note 'the'...obviously He intends a scalar...
# The same was in the beginning with God.
my $same; # Unused variable...typo?
}
# All things were made by him; and without him
# was not any thing made that was made.
my %things = make_all($word);# pass 'word' as maker.
# In him was life;
$word .= 'life'; # 'In' him??? Maybe concatenate
# and the life was the light of men.
$things{men}{light}=$word; # Something funny here...
# And the light shineth in darkness;
$things{darkness}{shine} = $word; # presume 'The light' means $things{men}{light}
# Is 'shineth' an attribute? leave for now.
# and the darkness comprehended it not.
die "unknown error" unless ok($all{darkness}); # leftover Debug code???
sub make_all { # will replace with Class::Factory...
my $maker = shift();
return map {$_=> make_thing($_,$maker) }
(qw(light darkness heaven earth seas stars sun men winged_fowl creeping_things)); #etc. etc. - see Genesis for full list
}
sub make_thing { # formless thing - add attributes later
my ($name,$maker) = @_;
return {
'name'=>$name, # should bless instead?
'maker'=>$maker # why? Copyright reasons??
};
}
</code>
<p>My initial thoughts were that it looks like something more generic that has been cut down.
That 'BEGIN' block looks a little suspicious - there's that unused variable...maybe this was
initally part of a Create($maker) (where $maker could be "The Force" or "Jehovah" or "Allah" or whatever) routine that's been hacked about.
<p>Also, there's something funny with that "{men}{light} = 'life'" thing.
I reckon there might have been a bit of a boo-boo, and he ends up assigning "Godlife" to "light"
rather than the other way round. As this then gets passed to "darkness", no wonder he was getting errors
in his test suite.
<p>So... refactoring and removing the "I made this, honest!" copyright stuff gives us something like this...
<code>
#!/usr/bin/perl
use strict;
use warnings;
use Earth::Things;
my %things = map {$_=> {'name'=>$_[0]} } (Earth::Things::list_all());
$things{men}{life}=$things{darkness}{shine} = "light";
</code>
...which I'm sure you'll agree is a lot neater, and a better base for expansion. Any volunteers for a sourceforge project?
<p>Ben.