Perl hacks were the subject of the August 10, 2006 meeting. If you have any interesting perl hackery you've done recently, feel free to post it here.

Calling a Function Using the 'Anonymous Subroutine' Prototype.

Here was the hack I brought with me. It uses a bit of an obscure perl feature to get some expressive results. Normally in perl one does not need to use or think about subroutine prototypes, however, here's a case where subroutine prototypes allow you to generate new perl syntax, allowing you to add brevity and clarity to otherwise tedious and ugly constructs. Much credit goes to Mark Jason Dominus (see his book Higher Order Perl) and Damien Conway (see Perl Best Practices).

The basic premise is using a prototyped subroutine with a bare ampersand as the first argument in the prototype. Note that since it's bare, the & is 'magical'. You work the magic when you pass in a reference to an anonymous subroutine. Below is an example:

sub foo (&) {
my $subref = shift;
## do stuff
}

How would you call this function? One of many ways:

## You could pass in a ref to a named function:
sub ack { print "hello\n" }
foo(\&ack);
## Or, you could create a ref to a subroutine, pass it in:
my $ack = sub { print "hello\n"};
foo($ack);
## (the same as above, but shorter)
foo( sub { print "hello"\n } );

The final way to call 'foo' is an interesting piece of perl arcana. This is the syntactic sugar that the prototype provides:

Example: Database Transaction Safe Block.

Here's a more interesting example. A function that provides a database transaction
safe block. Lets assume we have at our disposal a function called 'handle()' which
provides you the currently existing connection to your db.

Let's go overboard. You have multiple 'handle()' functions, each of which return
existing connections to different databases - handle1(), handle2(), handle3().
You want transaction safe blocks for all of them, but like a good lazy programmer
you loathe cutting and pasting. So that begs the question ... why write perl when
you can have perl write perl for you?

The function below generates functions that provide transaction safe blocks which
implicitly have access to the correct db handle.

OK, thats all for my hack. Feel free to add your own hack to this page.

-- Main.Matt - 11 Aug 2006

Proposed modification to Pod::Checker

The attached file (unified diff) against CPAN module Pod::Checker (Version 1.43) is an attempt to make two things happen:

Produce a Warning when only the

=cut

command is found.

Produce a Warning when a Pod command is found without a blank line preceding.

This is to identify Perl files (scripts, modules) that will go thru podchecker cleanly (rc=0 to shell), but will produce no actual documentation. The latter is triggered by sending "-warning" thrice to podchecker (or by having your own Pod::Checker front-end script set, e.g., options like -warnings => 3). Code snippets follow (new code in bluer text):

sub initialize {
my $self = shift;
## Initialize number of errors, and setup an error function to
## increment this number and then print to the designated output.
$self->{_NUM_ERRORS} = 0;
$self->{_NUM_WARNINGS} = 0;
#-----%<-------%<-------------------------------

sub end_pod {
## Do some final checks and
## print the number of errors found
my $self = shift;
------%<--------------------%<-----------
# no POD found here
$self->num_errors(-1) if($self->{_commands} == 0);