Hi All,

As part of my "get better with Perl" campaign, I have started running through cyber-dojo.com exercises when time permits. Tonight I took a run at the 100 doors exercise.

Doing it was good and educational "deliberate practise" for me. But I know form the office that code review is a great way to learn. So I turn to you oh PerlMonks community, please if you have some time and enthusiasm, please review my code (below) and tell me how you would improve it.

I am hoping to create code that is not "clever" rather "maintainable". So please review it in that light if you could (i.e. I'm not wanting to learn obfuscation).

Thanks!

doors.t

use strict; use warnings 'all'; use Test::More qw( no_plan ); require "doors.perl"; # Initialise array of doors. my @doors = init_doors(); is($doors[1], 'Closed', 'Door one should be closed'); is($doors[100], 'Closed', 'Door 100 should be closed'); toggle_door(\$doors[1]); is($doors[1], 'Open', 'Door one should be closed'); toggle_door(\$doors[1]); is($doors[1], 'Closed', 'Door one should be open'); my @answer = loop_through_doors(); # Doors open are: 1, 4, 9, 16, 25, 36, 49, 64, 81, and 100 is($answer[1], 'Open'); is($answer[4], 'Open'); is($answer[9], 'Open'); is($answer[16], 'Open'); is($answer[25], 'Open'); is($answer[36], 'Open'); is($answer[49], 'Open'); is($answer[64], 'Open'); is($answer[81], 'Open'); is($answer[100], 'Open'); # Check some of the other doors are closed. is($answer[2], 'Closed'); is($answer[3], 'Closed'); is($answer[50], 'Closed'); is($answer[99], 'Closed');
doors.perl:
use strict; use warnings; sub init_doors { my @doors; for my $i (1 .. 100) { $doors[$i] = 'Closed'; } return(@doors); } sub toggle_door { my $door_ref = shift; if( $$door_ref eq 'Closed' ) { $$door_ref = 'Open'; } else { $$door_ref = 'Closed'; } return $$door_ref; } sub loop_through_doors { my @doors = init_doors(); for my $pass (1 .. 100) { for my $visit (1 .. 100) { if (($visit % $pass) == 0) { toggle_door(\$doors[$visit]); } } } return(@doors); } 1;
You may also be able to look at it on Cyber-Dojo via the following link, I am not sure how long it remains live for however: Cyber-Dojo Link

In reply to Please review my code: 100 Doors. by lwicks

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!
  • 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, details, 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, summary, 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.