Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://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?

Ben.


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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-16 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found