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!

Saturday, 3 September 2011

DreamPlug HTTP Proxy

It looks like OpenVPN doesn't work too well with the iPhone so I'm going to try configuring an HTTP proxy so I can watch BBC iPlayer via my DreamPlug.

Since I have a fair amount of package manipulation to do, first I'm going to install Synaptic so I can use its GUI to see what's already installed and what's available.  This is pretty straightforward - apt-get install synaptic does the job.  And since I logged into the DreamPlug from my MacBook Air using ssh -X root@ I now just need to run synaptic to get going.  Excellent!

Next up, install the HTTP proxy.  I'm going to use Squid 2.7, though 3.0 is also available.  We also need apache2-utils to get the htpasswd program.

Now cd to /etc/squid and run htpasswd -c passwd to create a password for .

Now edit squid.conf and add these lines:
auth_param basic program /usr/lib/squid/ncsa_auth /etc/squid/passwd
acl auth_user proxy_auth REQUIRED
acl localnet src 192.168.8.0/24

http_access allow auth_user
http_access allow localnet
cache_dir null /tmp
cache deny all


And it works!

Tuesday, 30 August 2011

DreamPlug -- openVPN

I want to install openVPN on my DreamPlug so I can connect to the net from my iPhone over an open WiFi connection in a hotel without having to worry about eavesdroppers.  Step 1 is to install the package:

apt-get -u install openvpn

That seemed to work okay.  Wait for the edit to see what happened next...

DreamPlug -- first encounter

I bought a DreamPlug plug computer from www.newit.co.uk back in May and finally got round to plugging it in over the weekend.  The plug boots Ubuntu 9.04 Jaunty Jackalope from an internal uSD card, and starts up a WiFi AP straight away - SSID is DreamPlug-uAP-nnnn where nnnn is the last 4 digits of the MAC address.  After connecting to this you can ssh to the machine and login as root with a password of nosoup4u, and you can point a web broswer at the IP address to see the Lighttpd web server.

Forums are found at www.newit.co.uk/forum.  One handy post highlights a configuration problem with the package repository for Jaunty, which Canonical have moved.  It is fixed as follows:

cat > /etc/apt/sources.list << EOF
deb http://old-releases.ubuntu.com/ubuntu/ jaunty main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ jaunty-updates main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ jaunty-security main restricted universe multiverse
EOF

apt-get update

I tried that and it seemed to work okay.  (Incidentally, the URL was previously http://ports.ubuntu.com/).  Now for the tricky bit - update all the packages.  Here we go...

apt-get -u upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be upgraded:
  bzip2 cron dhcp3-client dhcp3-common dpkg file gzip language-pack-en
  language-pack-en-base libbz2-1.0 libcurl3-gnutls libgnutls26 libkrb53
  libldap-2.4-2 libmagic1 libnewt0.52 libpam-modules libpam-runtime libpam0g
  libsasl2-2 libsasl2-modules libsqlite3-0 libssl0.9.8 libvolume-id1 libxcb1
  lsb-base lsb-release ntpdate openssl perl perl-base perl-modules sudo udev
  whiptail
35 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 17.4MB of archives.
After this operation, 1528kB of additional disk space will be used.
Do you want to continue [Y/n]? y

That worked too!  Now, what shall I do next?

Wednesday, 18 May 2011

Modifying the UNIX environment in MacOS X

I wanted to tweak PATH and a few other environment variables after installing some software on my Macbook Air which runs MacOS X 10.6 Snow Leopard.  Here's what I discovered:

The environment is set up by /etc/profile.  This does 2 things: first it calls /usr/libexec/path_helper to set up PATH; then it calls /etc/bashrc to set up other environment variables for the shell.

The path_helper script works by first reading /etc/paths to get the default path elements (the file contains one directory path per line) and then by reading the files in /etc/paths.d (which are in the same format).

In the end I went for something simpler and put all my settings in $HOME/.bash_profile.  You should use this in preference to $HOME/.bashrc for setting your default environment because the latter file is run every time a shell starts, making things like export PATH="$HOME/bin:$PATH" have an undesired effect.

Saturday, 8 May 2010

Ubuntu Netbook Edition with Classic Desktop

My Sony Vaio VPC W12S1E netbook has a 1366x768 pixel screen,  so while I need Ubuntu Netbook Edition (known as Remix prior to 10.4 Lucid Lynx) for the Intel Atom processor support, I really don't need the netbook-specific desktop.

In the early days of UNR there was an applet called Desktop Switcher which let you jump between Desktop and Classic modes.  This was broken in 9.10 Karmic Koala and you had to jump through several hoops to get Classic back.

I was expecting similar difficulties with 10.4 Lucid Lynx and messed around uninstalling the ubuntu-netbook package and its dependencies to get what I wanted.  Having got it working I re-read the bug report and noticed the comment right at the top which said that 10.4
will come with the ability to choose a UNR or regular session at the  login screen as opposed to doing the switch live
 Sure enough, in the Session menu at the bottom of the login screen, you can select Gnome, UNE or xterm.  Doh!