Skip to content

Instantly share code, notes, and snippets.

@draegtun
Created March 22, 2012 10:23
Show Gist options
  • Save draegtun/2157592 to your computer and use it in GitHub Desktop.
Save draegtun/2157592 to your computer and use it in GitHub Desktop.
Saw this and just wanted to play!
#
# see http://perl.plover.com/idiocy/Addition.pm by MJD
#
use 5.014;
use warnings;
{
package Compute;
use Carp;
sub AUTOLOAD {
(my $name = our $AUTOLOAD) =~ s/^.*:://;
if ($name =~ m/^startWith(\d+)/) {
my $sum = $1;
while ($name =~ m/(\D+)(\d+)/g) {
given ($1) {
$sum += $2 when [qw/add plus/];
$sum -= $2 when [qw/minus subtract/];
}
}
no strict 'refs';
*$AUTOLOAD = sub { $sum };
goto &$AUTOLOAD;
}
croak "No compute!";
}
}
say Compute->startWith10add2minus4;
say Compute->startWith12minus4minus1plus2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment