Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Refactoring Perl applications to give them a rosy future

I'm doing some contract work at the moment to help sort out a Perl / CGI web solution that's been running well for a number of years, but needs enhancements and a route forward, and for which the original team's no longer available having moved into other full time employment. And it strikes me from looking through the code both how excellently the original work was done in terms of making it followable (quite an acheievement with Perl) and reliable, but also how old fashioned some of the elements are, and how that ties down and makes future developments potentially laborious. In fact I have a huge admiration from the exercise for the folks who did the laborious work to get it where it is today, and my heart bleeds at some of the simplifications and refactorings that I'm doing that cut away their work into somethng that's much shorter in code terms and going to be easier to maintain into the future.

A grand opening - so an examples of what I mean.

• Many of the messages are currently embedded in source - with HTML within Perl prints, giving rise to a complexity of double quotes within double quotes (add backslash protection) and variables within too. Much better (and I'm working on it!) to pull out the messages into separate message files, where the maintainer only needs to know HTML, and using a message system to feed them through. In fact [here] is the source of the message system. Duplication removed, complexity removed, capability added to cascade messages so that they can be amended / edited for individual customer groups or categories.

• Along the same lines, introduction of a unity of utility code for the provision of menu selection items - spotting a commonality that perhaps grew with the system rather than was intended from the start, and adding in as a result a consistency that the users (we hope and expect) will appreciate, as well as making updates simpler and code more followable. The new function looks like this:

  sub radio_offer {
    my ($question,$fieldname,@pairs) = @_;
    my $text = "<p>" . trim(main::message($question)) .": ";
    my $k;
    for ($k=0; $k<@pairs; $k+=2) {
      $text .= $pairs[$k] . "<input type=\"radio\" name=\"$fieldname\" value=\"$pairs[$k+1]\"/> &nbsp; ";
    $text .= "</p>";
    print $text;

THAT occurs just once and it's called up lots of times like this:

    "attended for review","review");
    "telephone","telephone", "Skype","skype", "online","online")

and there's another version radio_offer_sticky which will recheck boxes for you if a form is re-offered.

• Standardising on far fewer styles, and in places taking out something of a rainbow in favour of a cleaner crisper look. The system already has header and footer functions to give a page to page consistency, but taking these too into standard messages from within the code, and allowing the messages so involved to be changed per site.

• The code is a series of steps through forms spread over a handful of scripts. In areas where the workflow is round and round within that area, combine the scripts into a single script to there's no need to put up an error page and tell the user to click his back button. By working out the possible flows on the board (we've done that), we can step neatly in the right direction from within a single script.

• There's conditional code working out which page is next. I know a lot of time has gone into getting all the && and || conditions right - sometimes several lines of them - but it makes adding things very difficult indeed. Moving on to an error flag system which builds up each "or" element in turn, keeping track if any have gone wrong, makes for followable and updatable code, even if more statements. And by outputting the flag in the event of errors, both user and maintenance programmer will be told why they're being asked to review their previous input and not just that some mandatory box wasn't completed.

If you have code that needs refactoring - it could be a significant job, but don't be frightened and don't feel you need to move away from Perl to a more fashionable language - Perl provides the modernisation tools. Please get in touch with me if you would like to learn Perl of have some more advanced Perl advise. And I'm happy to spend a short time helping make suggestions for your systems too, but at present I've not got capacity for any further jobs line this (in fact if you're a skilled Perl person I know, please get in touch as I might be able to use a couple of days of help!). If you're reading this on the archive - i.e. in March 2015 or later - please do get in touch if you've got a project you need help on, as things might have got quieter!
(written 2015-01-11, updated 2015-01-13)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P774 - Perl - Good CGI scripting.
  [4320] An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20)
  [263] Harmony - (2005-03-31)
  [262] A little bit of fun - the new Perl Quiz - (2005-03-30)
  [261] Putting a form online - (2005-03-29)

Q915 - Object Orientation and General technical topics - Principles of Model - View - Controller
  [4691] Real life PHP application using our course training MVC example - (2016-06-05)
  [4641] Using an MVC structure - even without a formal framework - (2016-02-07)
  [4527] Hello Flask world / Python web micro framework - (2015-10-11)
  [4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
  [4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
  [4010] Really Simple Rails - (2013-02-17)
  [3919] What is a web framework? - (2012-11-10)
  [3705] Django Training Courses - UK - (2012-04-23)
  [3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
  [3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
  [3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
  [2612] The Model, View, Controller architecture (MVC) - what, why and how. - (2010-02-01)
  [2199] Improving the structure of your early PHP programs - (2009-05-25)
  [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)

Back to
Checking MySQL database backups have worked (not failed)
Previous and next
Horse's mouth home
Forward to
Interview conditions,and other instructions to staff
Some other Articles
Preparing data through a little bit of Perl
Philosophy behind object design - and how I applied in to a Java example
Splitting Java strings and extracting numbers
Interview conditions,and other instructions to staff
Refactoring Perl applications to give them a rosy future
Checking MySQL database backups have worked (not failed)
Cucumber example - test::unit, scenario outlines, datafile driven test
Global Regular Expression matching in Ruby (using scan)
Regression Testing my website - Cucumber and Watir
Guide exercise to help you learn Gherkin, Cucumber and Rspec
4736 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4391_Ref ... uture.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat