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 };


In reply to Cannot overload ++ when the object is accessed via Attribute -> Tie -> Class by mayaTheCat

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.