Chapter 25. Correcting Typos with Perl
Symbol::Approx::Sub is a Perl module that allows us to call subroutines even if we spell their names wrong. Using it can be as simple as adding this to your programs:
use Symbol::Approx::Sub;
Once we’ve done this, we never have to worry about spelling our
subroutine names correctly again. For example, this
program prints This is the foo
subroutine!
, even though &foo
was misspelled as &few
.
use Symbol::Approx::Sub; sub foo { print "This is the foo subroutine!\n"; } &few;
Why Was It Written?
This is obviously a very dangerous thing to want, so what made me decide to write Symbol::Approx::Sub?
In July 2000 I attended the O’Reilly Perl Conference and took
Mark Jason Dominus’s “Tricks of the Wizards” tutorial. He explained a
number of concepts that can take your Perl programs to a new level of
complexity and elegance. The most important of these concepts are
typeglobs and the AUTOLOAD
function. It was the first time
that I’d really tried to understand either of these concepts and,
thanks to Dominus’s clear explanations, I began to understand their
power.
One example that Dominus used in this class was a demonstration
of how we can use AUTOLOAD
to catch
misspelled subroutine names and perhaps do something
about it. He showed a slide containing code like this:
sub AUTOLOAD { my ($sub) = s/.*::(.*)/; # Work out what sub the user really meant $sub = get_real_name_of_sub($sub); goto &$sub; }
On the following slide, he went into some detail about what ...
Get Games, Diversions & Perl Culture now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.