in reply to Squeezing $a+1 to be magical like $a++

I'm not too familiar with overloading, so this is a "wonder if I can get that to work" snippet instead of a "I'd recommend doing it this way" suggestion.

Perhaps someone more familiar with overloading can point out the shortcomings of this quick hack. (other than not working for negative numbers, and the really poor algorithm of repeating $x++ 10 times for $x + 10)

#!/usr/bin/perl -wT use strict; package MagicalPlus; use overload '""' => sub { ${$_[0]} }, '+' => \&myadd, 'fallback' => 1; sub myadd { my $first = ${$_[0]}; my $second = $_[1]; $first++ while($second-->0); return addmagic($first); } sub addmagic { my $string = "".shift; my $self = \$string; bless($self, __PACKAGE__); } package main; my $x = MagicalPlus::addmagic "006"; # create our magic var for (1..10) { # watch it works its magic print "$x + $_ = "; $x = $x + $_; # magical? print "$x\n"; } =OUTPUT 006 + 1 = 007 007 + 2 = 009 009 + 3 = 012 012 + 4 = 016 016 + 5 = 021 021 + 6 = 027 027 + 7 = 034 034 + 8 = 042 042 + 9 = 051 051 + 10 = 061

-Blake