Today I found something interesting. I've used overloading in the past without trouble but today wasn't getting stringification to work. I reduced my problem to the following test case:
package SomeClass; use overload '""'=>"name"; sub new { return bless {}, shift; } sub name { my $self = shift; $self->{NAME} = shift if @_; return $self->{NAME}; } 1;
And here's my test script to use the class above:
use lib '.'; use SomeClass; my $thing = SomeClass->new; $thing->name('TestObj'); print "Explicit: my name is ", $thing->name, "\n"; print "Implicit: my name is $thing\n"; exit;
The second print statement printed "Implicit: my name is " whereas the first print worked as desired, including the name.
I tested the above under both 5.8.0 Linux-multi-thread and 5.8.0 MSWin-multi-thread and eventually came to realize that I could get it to work by pointing my overload directive to a third function in SomeClass:
sub stringify { my $self = shift; return $self->{NAME}; }
Any ideas why my first attempt failed? I can't see any good reason why my stringify() method works but not name(). Thanks in advance.
[Jon]
In reply to Overloading oddity by theguvnor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |