Not at all sure why anyone would want to do this - I really wrote it to see how well I understood typeglobs and AUTOLOAD :)

What it does is allow you to call subroutines in your script using names that only match approximately. Currently this means that they have the same soundex value as generated by Text::Soundex.

As I said above, there's really no good reason at all why you'd ever want to do this and it will make your code completely unmaintainable, but it's just a bit of fun. Here's a sample program showing how you'd use it.

use strict; use Sub::Approx; sub aa { print "You called 'aa'\n"; } &a;

In this example, &a doesn't exist so &aa gets called instead as 'a' and 'aa' both have the soundex value of 'A000'

Enjoy...

Update:

I've uploaded this module to CPAN. You can get it at search.cpan.org/search?dist=Symbol-Approx-Sub

package Sub::Approx; use strict; use vars qw($VERSION $AUTOLOAD); use Text::Soundex; $VERSION = '0.01'; sub import { no strict 'refs'; # WARNING: Deep magic here! my $pkg = caller(0); *{"${pkg}::AUTOLOAD"} = \&AUTOLOAD; } sub AUTOLOAD { my %cache; my @c = caller(0); my ($pkg, $sub) = $AUTOLOAD =~ /^(.*)::(.*)$/; no strict 'refs'; # WARNING: Deep magic here! foreach (keys %{"${pkg}::"}) { my $glob = $::{$_}; $glob =~ s/^\*${pkg}:://; push @{$cache{soundex($glob)}}, $glob if defined &{"*${pkg}::$glob +"}; } $sub = soundex($sub); if (exists $cache{$sub}) { my @f = @{$cache{$sub}}; $sub = "${pkg}::$f[rand @f]"; goto &$sub; } else { die "REALLY Undefined subroutine $AUTOLOAD called at $c[1] line $c +[2]\n"; } } 1;

In reply to Call Subroutines by Approximate Name by davorg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.