Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Tutorial: Introduction to Object-Oriented Programming

by adrianh (Chancellor)
on Dec 13, 2002 at 12:27 UTC ( [id://219566]=note: print w/replies, xml ) Need Help??


in reply to Re: Tutorial: Introduction to Object-Oriented Programming
in thread Tutorial: Introduction to Object-Oriented Programming

Your right - forgotten about that.

You still have the problem of overloading tho - what if a subclass overloads "+"?

Unfortunately, there isn't an overload::NumVal to match overload::StrVal (unless I'm missing something somewhere ;-)

You can get around this by blessing the object into another class you know isn't overloaded, get the numeric ID, and then bless the object back into it's original class. For example:

package Foo::Base; use strict; use warnings; sub self { my $self = shift; my $package = ref($self); bless $self, __PACKAGE__; my $id = $self+0; bless $self, $package; return($id); }; package Foo; use base qw(Foo::Base); use strict; use warnings; my %foo = (); sub new { my $class = shift; bless [], $class; }; sub foo { my $self = shift->self; $foo{$self} = @_ if @_; $foo{$self}; }; sub DESTROY { my $self = shift->self; delete $foo{$self}; };

Mildly evil - but seems to work.

Replies are listed 'Best First'.
Re: Re: Re: Tutorial: Introduction to Object-Oriented Programming
by demerphq (Chancellor) on Dec 15, 2002 at 01:45 UTC
    Unfortunately, there isn't an overload::NumVal to match overload::StrVal (unless I'm missing something somewhere ;-)

    I think the best way to do this is Scalar::Util::refaddr. And its much faster than parsing overload::StrVal.
    :-)

    But it isn't core (at 5.6 anyway)
    :-(

    --- demerphq
    my friends call me, usually because I'm late....

      You mindreader you! I was just on my way to update the node after discovered refaddr!.

      It's much faster than my reblessing hack too.


      Update: It isn't much faster. In fact, it's almost exactly the same as my quick hack. Goodness knows what I wrote in my benchmark script :-)

      Another update: It is much faster if you have the XS version. It isn't if you don't :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://219566]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-19 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found