Turtle hibernation with Perl

December 12th, 2010 robin Posted in Ironman, Just for Fun, Perl Comments Off on Turtle hibernation with Perl

We have two red-eared slider turtles.  For most of the year they live in our living room in a big aquarium, but for the winter they have to go into hibernation.  As the winter approaches, the light and temperature in the aquarium has to be reduced, and then around the beginning of December they have to go into complete darkness, at about 10C.  Last year we tried putting them into the cellar: our cellar was too cold (went down to just +1C, could have frozen…), my girlfriends parents cellar was too warm, and in the end our neighbours cellar had about the right temperature… but a lot of bother.

So this year I thought I’d find a technical solution.

  • styrofoam and spray foam around a storage box to make a really well insulated box for the turtles.
  • Two CPU water cooling blocks sandwiching a Peltier element (thermoelectric heat pump).
  • From the cold side of the heat pump a water cycle through a copper pipe in the turtle box to cool the box
  • From the hot side of the heat pump a water cycle through a radiator to cool the water
  • A networked atmega processor to read the values from two thermometers (one in the cool water cycle, one in the turtle box), and turn the pumps and peltier element on and off.
  • A Perl script to control it!

When I’ve got the logic and parameters worked out, I’m going to translate the logic of the Perl script to C and load it onto the atmega processor, but for starters Perl is a great helper because I can quickly prototype, try out and refine without having to recompile, and to be honest… I just don’t really like C as much.

And as a by-product of monitoring and controlling the system with Perl, I was able to easily store the data in an SQLite database, and use GD::Graph to create pretty graphs to see how healthy the system (and of course the turtles) is:

This is what it looks like:


XML parsing with Perl

December 8th, 2010 robin Posted in Ironman, Perl 3 Comments »

XML is a ubiquitous format for data transfer, configurations, and so much more. Processing it can be easy, but it can also be a pain. As always with Perl, there is always more than one way to do it… So I’ve created a framework to test, and compare some of the modules available.

The modules I considered were:

XML::Simple
XML::Smart
XML::Parser (using Tree style)
XML::Twig

My tests take a given XML (a list of films as generated by Mediathek), parse out the individual film entries, and print them out to a csv file. I don’t actually need this, but it’s a typical functionality you might need when working with XML, and allows some good comparisons with both small and huge file sizes.

Considerations:

The main difference between the clever modules (XML::Simple and XML::Smart) and the more grass-roots (XML::Parser and XML::Twig) is the method they use to work through the XML. To be clever, the clever ones suck in the whole XML, analyse it, and create a good hash/array representation of the elements. This has the advantage of having an easy to use format to work with afterwards, but also requires sucking in the whole XML in one go: not a problem for small files, but it quickly becomes dangerous because the system will use 10-20 times the file size in memory: a 60MB file can easily consume 1GB in memory! XML::Twig by contrast works through the file element by element, and so its memory usage is very controllable: it is possible to parse hundreds of megabytes without the perl application expanding beyond 12MB in memory! On the other hand: the clever modules are much easier to use, and you don’t need to know anything about your XML to process it. That makes these modules much more attractive for small (e.g. configuration) files.

For the details of the implementation, please have a look at the code examples:

git clone git@github.com:robin13/rcl-ironman.git

My tests showed these relationships, though your mileage might vary – it depends a lot on the depth/complexity, and variance in size (big on Mondays, small on Thursdays?) of the XML you are dealing with.

By Speed (seconds to process a 66MB file)

XML::Parser   26.5
XML::Simple   76.0
XML::Twig    132.8
XML::Smart   394.9

By Usability (subjective) to implement

XML::Simple     Easy
XML::Twig       Usable
XML::Parser     Complex
XML::Smart      Not so smart...

By Memory consumption (system memory in kB used for a 66MB file)

XML::Twig        972
XML::Parser   506532 (using Tree style)
XML::Simple   628268
XML::Smart   1336604

To summarise:

If you want high performance: XML::Parser
If you want relatively easy, memory efficient parsing of huge files: XML::Twig
If you want easy-to-implement for small files: XML::Simple
If you want to have a bad deal: XML::Smart

What I left out:

Probably a lot of usable modules…  If you know of something important, please comment!
Input/Output: XML::Simple and XML::Parser can output XML too.  That might be important for you (e.g. config files)!
My code is proof of concept, not highly optimised: feel free to improve, or even add other modules to the tests.  Git makes that easy! 🙂