Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

mir (51)



Journal of mir (51)

Friday April 17, 2009
05:06 AM

DateTime tip

Note to self: add and subtract modify the calling object. Use clone if you just want to use the result.

So do not write this:

my $today      = DateTime->now;
my $yesterday  = $today->subtract( days => 1);# changes $today

Instead write this:

my $today      = DateTime->now;
my $yesterday  = $today->clone->subtract( days => 1); # note the 'clone' call

Sunday July 20, 2008
12:42 PM

On OS's and user friendliness

So I just got this USB 3G device, a modem that's supposed to connect me to the World when I am out here on the Tuscan hills. Of course it came with Windows drivers and I don't have a Windows machine, so I plugged it on the Mac and hoped it worked, as usually happens... no dice. You need a driver. Then I tried to install the linux driver that Vodafone gently provides on the (linux) EEE... but I was missing some libraries, and I couldn't get it to run. Grrr... yet an other case of Linux being unfriendly to recent hardware I guessed!

Then finally "en de'sespoir de cause", I just plugged the device into the darn machine... and low and behold... it Just Worked (tm)! The EEE comes with a piece of software that recognizes my modem, brings up a nice popup and connects me to the rest of the World, from that remote, DSL (and phone!)-deprived area on the Apuan Alps. Yay for Linux! Friendlier than the Mac (and even Windows needs some additional piece of software to be installed I think).

Sorry Pudge...

Wednesday May 07, 2008
11:15 AM


An other reason why subroutine should always end with an explicit return.

XML::Twig has this XML::Parser handler for characters that ends (or rather used to end!) with $elt->{pcdata} .= $string;. This is the common case, but it's hidden in the middle of a few if/else's, and it's actually an inlined method call. So it's not obvious what's going on there. But this was what happened when the XML to be parsed included a 4Mb, 60K line, base-64 encoded element: the handler was called 120K times (once for each line, once for each line return). For each of those calls the current content of the element was returned by the handler, and promptly discarded in the bowels of XML::Parser. Except that if you count 120 000 * 4MB/2, that makes nearly 500 GB of memory that needed to be allocated, copied and discarded, for absolutley no good reason at all.

In the end, adding a return at the end of the handler took processing time from 581s to... 2s. It probably improves speed in less specific cases.

And yes, it is one of Perl Best Practices recommendations (although not for performance reasons). So were was the PBP in 1997 when I wrote the first version of XML::Twig?