Main Content

Bellringing and Programming and Objects and Perl

Archive - Originally posted on "The Horse's Mouth" - 2005-02-25 04:34:54 - Graham Ellis

For the second time this year (and it's only the second month), I find that I have a keen bell-ringer on my programming course ... he's off each evening to meet up with fellow bell-ringers in church towers in Wiltshire, and having a really good time outside course hours as well as during the day. I understand that there are a substantial number of excellent sets of bells (I know that the word "set" is wrong) in this area, and I listen in some awe to the complexity described in ringing sequences - how do you get from a string like x16x36x36 to a set of results like:
1 2 3 4 5 6
2 1 4 3 6 5
2 4 1 6 3 5
4 2 6 1 5 3
2 4 6 5 1 3
4 2 5 6 3 1
2 4 5 3 6 1

With so many questions like this one - "how do I get from 'A' to 'B'" - you only need one expert to tell you how to do it, write down and test the algorithm, and then anyone who can provide inputs and read outputs can use the algorithm. In Object Oriented terms, this "hiding information within" approach is known by the term "encapsulation".

Helped by 'George' and 'Sandra', I've learn a little about ringing in the last weeks and yesterday George and I put together a sample program to ring some parts of bell changes, using Perl. Not exactly what he'll be using Perl for at work, but a parallel through which he can practise and learn further. Here's some of the code:

use bells;

push @method,new bells("Plain Bob",8);
push @method,new bells("Grandsire Triples",7);
push @method,new bells("St Clements",6);

$method[0]->setplace("x18x18"); # etc ...
$method[1]->setplace("3.1.7.1.7"); # etc ...
$method[2]->setplace("x16x36x36"); # etc ...


We've set up three methods of ringing, by name and by the number of bells involved in the changes. We've then called a method called "setplace" to define how the changes happen in place notation. Let's then ring the changes:

foreach $curr_method(@method) {

Printing out a header for each set of changes, and getting back an "action list" of what we need to apply:

@chlist = $curr_method->getchanges();
$method_name = $curr_method->getname();
print "Ringing $method_name\n";
@order =$curr_method->getcurrent();
print "@order\n";


And then actually ringing the changes are reporting the order of the bells.

foreach $chan (@chlist) {
$curr_method->change($chan);
@order =$curr_method->getcurrent();
print "@order\n";
}
}


Yes, that's all there is in my main application! The code that took us a while to work out which interprets the place notation strings and applies it to the rings of bells is all hidden within the file bells.pm which we called in at the top. The typical user doesn't need to know how it works. All they want to do is to run a program like the one above and read:

Ringing Plain Bob
1 2 3 4 5 6 7 8
2 1 4 3 6 5 8 7
2 4 1 6 3 8 5 7
4 2 6 1 8 3 7 5
4 6 2 8 1 7 3 5
Ringing Grandsire Triples
1 2 3 4 5 6 7
2 1 3 5 4 7 6
2 3 1 4 5 6 7
3 2 4 1 6 5 7
3 4 2 6 1 7 5
4 3 6 2 7 1 5
Ringing St Clements
1 2 3 4 5 6
2 1 4 3 6 5
2 4 1 6 3 5
4 2 6 1 5 3
2 4 6 5 1 3
4 2 5 6 3 1
2 4 5 3 6 1

I HAVE placed the bells.pm on our website if you would like a look - there's a link to it from our Object Oriented Design - Individual Object module as this complex field has proven to be such a great example of the use of encapsulation, even on a quite small project.