Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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 Genesis before (which I think may actually be part of the Vedas:: namespace), but I wanted to check out the main 'init()' routine.

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.

#!/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 $thing +s{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?? }; }

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.

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.

So... refactoring and removing the "I made this, honest!" copyright stuff gives us something like this...

#!/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";
...which I'm sure you'll agree is a lot neater, and a better base for expansion. Any volunteers for a sourceforge project?


In reply to Rewriting creation (with strict and warnings) by benn

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, 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
  • 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (8)
    As of 2021-04-16 15:30 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found