Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
Designing your data structures for a robust Perl application

Whatever language you're programming in, design of your data structures is important. You should consider such design ahead of time, and before you start to code - "What am I going to be doing with this data" and "how do I want to access it".

It is easy - VERY easy - to fall into the trap of staring to code without adequate thought (and a diagram) and in language like Perl (especially) which assumes you know what you are doing, and where a few bytes of code can do a lot of work, you can so easily head for trouble if you're unplanned.

So here is a piece of PLANNED code! I am going to be writing code to analyse a log file. It's a web server's access log file, where each request is a separate line in the file, with the first field on each line identifying the visiting client computer.

My data design:

• I want a HASH, keyed by the visiting client computer's identity (IP address).

• the values in that hash are to be references to a LIST of accesses from that client and

• each access record is itself to be a list of individual strings from the incoming access records.

So - in summary - a Hash of lists of lists.

Design done - let's NOW write the setup code!

open (FH,"ac_20090818") or die;
while (<FH>) {
  my ($thisip,@otherparts) = split;
  push @{$all{$thisip}}, \@otherparts;

Do not be mislead by how short that code is - it really does set up the three-tier structure I described. What a good job I HAD described it, though, so that it's easy to handle. Let's now test it, by printing out part of one record and also a summary of the number of visitors:

# For IP address, look at the
# 7th hit and tell us the 6th fld.
print ${${$all{""}}[6]}[5],"\n";
print $all{""}->[6]->[5],"\n";
print $all{""}[6][5],"\n";
@visitors = keys %all;
print "Visits from ",@visitors+0," places\n";

and running that:

Dorothy-2:pl grahamellis$ perl actab
Visits from 14872 places
Dorothy-2:pl grahamellis$

What if your code is going to be more than just a few lines long? Are you going to be able to design / recall / easily code structures like these? Probably not - you'll want to use an approach that is more extensible. And that's where you'll take the complicated logic bits inside and hide them ("encapsulate them") within a module or a class - Structured and Object Oriented Programming which allows you to go from small to medium and large applications robustly, and without writing code that becomes a nightmare to enhance.

The example above was written at the end of yesterday's opening day of our Perl for Larger Projects class. I'll be carrying on with it today - moving to an Object Oriented application where each of the layers will be written with more straightforward, verifiable, testable, re-usable code - leading towards an application in which we can extract and report on information about our web site visitors with ease ... and which we can easily enhance and modify as further analyses are required.
(written 2009-08-25, updated 2009-08-26)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P704 - Managing Perl Projects
  [2070] Converting to Perl - the sort of programs you will write - (2009-03-08)
  [1395] Dont just convert to Perl - re-engineer! - (2007-10-18)
  [836] Build on what you already have with OO - (2006-08-17)

P711 - An Introduction to Standards in Perl
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [2875] A long day in Melksham ... - (2010-07-17)
  [2688] Security considerations in programming - what do we teach? - (2010-03-22)
  [1863] About dieing and exiting in Perl - (2008-11-01)
  [1853] Well structured coding in Perl - (2008-10-24)
  [1728] A short Perl example - (2008-07-30)
  [1555] Advanced Python, Perl, PHP and Tcl training courses / classes - (2008-02-25)
  [1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
  [1221] Bathtubs and pecking birds - (2007-06-07)
  [1047] Maintainable code - some positive advice - (2007-01-21)
  [965] KISS - one action per statement please - Perl - (2006-12-05)
  [945] Code quality counts - (2006-11-26)
  [743] How to debug a Perl program - (2006-06-04)
  [668] Python - block insets help with documentation - (2006-04-04)
  [242] Satisfaction of training - (2005-03-11)

Back to
Lead characters on Perl variable names
Previous and next
Horse's mouth home
Forward to
Long job - progress bar techniques (Perl)
Some other Articles
Making variables persistant, pretending a database is a variable and other Perl tricks
Handling XML in Perl - introduction and early examples
Wiltshire / Melksham Weddings - guest accommodation
Long job - progress bar techniques (Perl)
Designing your data structures for a robust Perl application
Lead characters on Perl variable names
Translation from Ghanaian to English
Public Transport from (and to) Melksham on Sundays
Quiet summer days? I think not!
C++, Python, and other training - do we use an IDE
4750 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/2375_Des ... ation.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat