adamcrussell

Perl Weekly Challenge 018

So much use of Class::Struct!

I never really used Class::Struct before. When I needed a simple object Perl's basic object system was always fine and when I needed a slightly more complex object Perl's basic object system is always fine. I am no stranger to how other languages organize their object oriented constructs and something in this week's challenge made me recall the use of C structs. Once I started using Class::Struct in a way that made sense I started to see just how much I could do with this convenient module. The code that follows has an interesting, not particularly Perlish look to it. Almost a Java like style because of what might be called "inner classes" (inner structs?).

Part 1

I used what are called Suffix Arrays to solve this week's Part 1. Suffix Arrays are a significant topic and I will have to defer and in depth discussion of their use for another time. In particular, my implementation SuffixArray.pm is a lengthy file which is not easily included in this blog so I am only linking to it from here. 

Sample Run

$ perl -I. -MSuffixArray ch-1.pl ABCAA BABC
ABC

Part 2

ch-2.pl
ch-2.pl

ch-2.pl is really just making some calls to PriorityQueue.pm which is doing all the work. That code is below.

PriorityQueue.pm
PriorityQueue.pm

Sample Run

$ perl -I. ch-2.pl
eat pizza
work on perl weekly challenge
work on blog
go to the gym
drink water
clean dishes
sleep

Discussion 

Here you can see what I meant earlier by "inner classes". Node and Heap are packages defined within the PriorityQueue package. All packages use Class::Struct, even the Priority Queue package. The implementation of a Priority Queue follows the classic C style approach which inspired the use of Class::Struct to begin with. It turns out Class::Struct is quite versatile and works with use overload, which is part of my SuffixArray implemenation in Part 1.


Comments for this post were locked by the author