mayaTheCat has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

Nowadays, I am trying to develop a module which adds the following capability to a number storing scalar: In numeric context the scalar returns the number, and in string context it returns the stringified version of the number - in other words the name of the number.

The typical usage I plan is as follows;

use warnings; use strict; use Test::More tests => 6; use Attribute::StringifiableInteger; my $integer : StringifiableInteger = 1; ok($integer == 1); is($integer, 'one'); is(sprintf("%d",$integer), 1); is(sprintf("%s",$integer), 'one'); $integer = 3; $integer += 1; is($integer, 'four'); $integer++; is($integer, 'five');
My problem begins with the last two lines of the example above; the increment operator is not overloaded as I expected.
The relevant part of the module code is as follows;
use warnings; use strict; package StringifiableInteger; use Carp; use overload '""' => \&_to_string, '0+' => \&_get, '++' => sub { my ($self) = @_; $$self += 1; return 'looks like the return value is not used.'; }, fallback => 1, ; my $regex_integer = qr{ ^ -? \d+ $ }x; sub new { my ($class) = @_; my $integer; bless \$integer, $class; } sub set { my ($self, $value) = @_; croak "invalid integer value ($value)" unless $value =~ $regex_int +eger; $$self = $value; } my @stringified = qw/zero one two three four five six seven eight nine + ten/; sub _to_string { my ($self) = @_; return $stringified[$$self]; } sub _get { my ($self) = @_; return $$self; } package TieStringifiableInteger; sub TIESCALAR { my ($class) = @_; my $integer = StringifiableInteger->new; bless \$integer, $class; } sub STORE { my ($self, $value) = @_; $$self->set($value); } sub FETCH { my ($self) = @_; return $$self; } package AttributeStringifiableInteger; use Attribute::Handlers; sub UNIVERSAL::StringifiableInteger : ATTR(SCALAR) { my ($referent) = @_[2..2]; tie $$referent, 'TieStringifiableInteger'; } 1;
I wonder whether there is a way to overload the ++ operator or not.
I also have another question;
What is your opinion on such a module? Is it worth uploading to CPAN?
A note; I am designing to module to add stringification for different languages. Like;
use Attribute::StringifiableInteger lang => 'tr';
or,
my $integer : StringifiableInteger('tr');

Thanks in advance,
Oguz

---------------------------------
life is ... $mutation = sub { $_[0] =~ s/(.)/rand()<0.1?1-$1:$1/ge };

Replies are listed 'Best First'.
Re: Cannot overload ++ when the object is accessed via Attribute -> Tie -> Class
by Anno (Deacon) on Oct 03, 2007 at 13:19 UTC
    My, my my. Overload, wrapped in a tie, wrapped in an attribute. That doesn't make debugging easier. If you have trouble with such a setup, you need to peel back the various layers and test each one separately, first overloading, then the tie interface, then the attribute wrapper.

    I wonder whether there is a way to overload the ++ operator or not.

    The increment operator *is* overloaded as systematic testing shows. The problem is that the TieStringifiableInteger::STORE method is called with the stringified version of the new value instead of the numeric one. (No, I don't know offhand why that happens.) Change it to

    sub STORE { my ($self, $value) = @_; $$self->set(0 + $value); }
    That forces the numeric value and fixes the error.

    As a side note, is there a reason why two of the overloading functions ('""' and '0+') are named methods but '++' is implemented anonymously?

    What is your opinion on such a module? Is it worth uploading to CPAN? A note; I am designing to module to add stringification for different languages.

    In my view, the module has an overly involved interface to call a function automatically that would better be called explicitly from case to case. The basic function (converting numbers to strings) is already covered by CPAN modules for various langiages, including Turkish (Lingua::TR::Numbers)

    Anno

      Thanks for the solution; it worked.

      Reply for the side note: the function being anonymous was just temporary. Since there is a solution now, the function has the right to have a name :)

      Finally, thanks for the CPAN alternatives.

      My motive in developing this module is to play with Attributes.

      Oguz

      ---------------------------------
      life is ... $mutation = sub { $_[0] =~ s/(.)/rand()<0.1?1-$1:$1/ge };