On 09/16/2012 03:46 PM, jmrhide-p...@yahoo.com wrote:
OK, here's my amateurish attempt at a correction:

i know you said you don't want to learn to be a professional coder. but there are so many ways to improve your code little by little. most here have been shying away for good reason and because you don't seem to want to do more work than you have to.

#!/usr/local/bin/perl

use warnings;
use strict;
use diagnostics;

drop the diagnostics that as it isn't important if you already understand the errors.


use CGI;
my $cgi = new CGI;

use Mail::Sendmail;

use CGI::carp qw(set_die_handler);
     BEGIN {
     sub handle_errors {
     my $msg = shift;
     sendmail(
     From     => 'i...@theinfosite.com',
     To         => 'jmrhide-theinfos...@yahoo.com',
     Subject   => 'Tutorial Feedback',
     Message => $msg,
     );
     }
     set_die_handler(\&handle_errors);
     }

my (
     $topic,     $score,     $lastnum,  $answer,      $anum,
     $bnum,      $cnum,      $dnum,     $enum,        $smarts,
     $playlevel, $c_topic,   $c_smarts, $c_playlevel, $c_score,
     $c_lastnum, $c_goodans, $stimulus, $texta,       $textb,
     $textc,     $textd,     $texte,    $defnum,      $smiley,
     $feedback,  $goodans,   $restart,  @data,        @cat,
     @term,      @def,       @story,    @candidate,   @termnum,
     $sound
);

that is a major weakness in your code. don't declare vars in a wider scope than is needed. iirc from seeing full versions, you have very few subs in your code. that is the first pass at cleaning things up. put all logical sections of your code into subs. then call those from the top level. you should have very little code at the top level of any script. really no more than 5-10 lines at the top of the file. all other code should be in subs. this allows you to organize things, edit individual subs, keep tighter scoping, reuse code, move some subs to modules, make for easier debugging, etc. the list of wins is ginormous.

if the only thing you take from here and do to this script is to put most of the code into subs and move most of those lexicals to their proper sub scopes, it will be a major advance by you. as analogy the body is divided into organs for specialization and organization. code likes to be divided up that way too. i used that analogy as you are a doctor and might have some understanding of anatomy and physiology. :)

thanx,

uri

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to