Tuesday, 11 June 2013

Printing BIIIIG tables with CGI.pm

I write a lot of web pages using Lincoln Stein's wonderful CGI.pm Perl module.  The guy has thought of *everything*!!  Today, however, I came across a problem that I thought I'd have to kludge my way out of.  I wanted to print a table, so I did the usual:-
print table(thead(Tr(th(...), th(...))), tbody(Tr(td(...), td(...), ...)));
Unfortunately the contents of the table tag were somewhat huge, so Perl grabbed most of the memory on the web server in its attempt to prepare the string for printing, causing all sorts of nastiness.

What I needed was some way to generate and print the table tag without the table contents and end tag, and ideally the same for tbody, and maybe thead and even tr for good measure.  I thought I'd have to generate the table with a dummy placeholder and split the string manually, something like this:-
my $table_container = table('CUT HERE FOR PRINTING');
then find my string, print the bit before it, print the table contents, then print the bit after it.  Yuk!!  So I resorted to Google for inspiration and tried "cgi.pm start_table".  Hey, presto!  There was the answer, in the CGI.pm documentation itself, and it uses start_table as the example:-
use CGI qw/:standard *table start_ul/;
This generates functions start_table, end_table, start_ul and end_ul which behave just the way you'd expect.  In hindsight there's a clue to this, because the standard function set includes start_html and start_form, so good ol' Lincoln was well ahead of the game.

Right, back to that CGI script...

Friday, 7 June 2013

Reading O'Reilly digital publications on a Mac

I've taken advantage of O'Reilly's $4.99 eBook Upgrade offer to convert my shelf of paper tomes into a folder of shiny new eBooks.  They come in ePub format which is great for my iPhone and iPad, and Mobi which works well on my Kindle.  For the MacBook I've been using PDF which is okay, but suffers in readability terms because the text doesn't reflow to fit the page and font size.

I tried the OS X Kindle Reader app, which is a free download from the App Store, but it's a bit slow and crashy.  So I thought "there must be an OS X equivalent of iBooks to read the ePub files", but Apple haven't got around to doing that yet.  So now I'm trying Adobe Digital Editions.  First impression is that it's slicker than the Kindle Reader. I'll let you know how I get on.

Another tip for O'Reilly eBooks - you can sync them to your Dropbox account so they're always available to your iDevice.  Neat!