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;
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: Call Subroutines by Approximate Name
by merlyn (Sage) on Jul 26, 2000 at 19:13 UTC | |
by davorg (Chancellor) on Jul 26, 2000 at 19:21 UTC | |
Great for obfuscation...
by myocom (Deacon) on Jul 27, 2000 at 03:49 UTC | |
RE: Call Subroutines by Approximate Name (poetic license)
by osfameron (Hermit) on Aug 07, 2000 at 00:13 UTC | |
by tilly (Archbishop) on Aug 07, 2000 at 01:48 UTC | |
by osfameron (Hermit) on Aug 07, 2000 at 02:34 UTC | |
by tilly (Archbishop) on Aug 07, 2000 at 03:02 UTC |