Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Using overload. Any complete (simple) examples?

by BrowserUk (Pope)
on Oct 07, 2003 at 19:50 UTC ( #297372=perlquestion: print w/replies, xml ) Need Help??
BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

I've been playing around with overload for a while now, but always seem to encounter some problem.

It seems I'm always forced to overload '""' whether I really want to or not? And it seems to be called everytime the object is referenced even if it is being dereferenced?

I've never managed to get the numify '+0' overloading to do anything.

Anyone know of any fairly complete but reasonably simple examples of overloading? I looked at the Math::Big* modules, but they are so complex as to make it difficult to pick out the vagries of the overloading.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

  • Comment on Using overload. Any complete (simple) examples?

Replies are listed 'Best First'.
Re: Using overload. Any complete (simple) examples?
by LD2 (Curate) on Oct 07, 2003 at 20:09 UTC
    I'm not sure if this will help or not... but take at look at this article on overloading over at Perl.com.

      LD2++ Thanks muchly for the pointer. And ++davorg also.

      I *think* (after a first reading) that the bit I have been missing, despite having read (scanned) it many times is the overload::constants and the overload::remove_constant() stuff.

      The article and associated module where exactly what I was looking for.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail

Re: Using overload. Any complete (simple) examples?
by jeffa (Bishop) on Oct 07, 2003 at 20:10 UTC

      Thanks Jeffa. Believe me, I haven't forgotten about (jeffa) Re: A better mod (%) operator? and my reply at Re: (jeffa) Re: A better mod (%) operator? still stands. I've frequently used overload for individual and groups of math and boolean operators, probably because you gave me 'boost up' back then.

      However, whenever I've tried to use it with the more esoteric operators, notably, '""' and '0+' (Thanks Zaxo) but also "{}", "[]", I've often found myself fighting the compiler to get the effects I want ... as opposed, perhaps, to the effects that overload is designed to give me.

      I've read the overload docs so many times I almost know them verbatim, but I still get caught out every time I try to use it....as at Temporarily disabling overloaded operations..

      Individually, I can make them work fine (mostly:), but when I try to use large scale overlaoding, especially in classes that are not simple classes, but (for example) use inheritance, or even tieing, things start going astray.

      So, having looked around at a few complex uses like the Math::Big* suite, but found it difficult to follow as the are inherently complex beasties without the overloading, they also aren't pure perl, which makes it more difficult to trace stuff through in the debugger, I thought I'd ask if anyone knew of a fairly simple, but (more) complete worked example.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail

Re: Using overload. Any complete (simple) examples?
by naholos (Scribe) on Oct 07, 2003 at 20:16 UTC
    Here is a simple Message object We use in our office: It stringifies into a simple message, or as a number will give a code, and evaluates as true or false as dictated by the user. It also allows you to push on some extra data with push, for debuging or general extra information on a message.

    example of use:
    #Message->new(<Number>,<MSG>,<BOOL>);
    Message->new(12345,'This is my Message or error message',0);

    package Message; use strict; use overload '""' => 'str', '0+' => 'num', 'bool' => 'bool', fallback +=> 1; ###################################################################### +######### sub new { my $class = shift; my $num = shift; my $str = shift; my $bool = shift; my $data = {num => $num, str => $str, bool => $bool, data => [], }; if ( ! defined $data->{bool} ) { $data->{bool} = ( $data->{num} ? 1 : 0 ); } bless $data, $class; if ( @_ ) { if ( ref $_[0] eq 'ARRAY' ) { foreach my $element (@_) { $data->push($element); } } else { $data->push(@_); } } return $data; } ###################################################################### +######### sub push { my $self = shift; my $obj = shift; push @{$self->{data}},$obj; } ###################################################################### +######### sub num { my $self = shift; return $self->{num}; } ###################################################################### +######### sub str { my $self = shift; return $self->{str}; } ###################################################################### +######### sub bool { my $self = shift; return $self->{bool}; } ###################################################################### +######### 1;
Re: Using overload. Any complete (simple) examples?
by Zaxo (Archbishop) on Oct 07, 2003 at 20:23 UTC

    Probably a typo, but the overload tag for numification is '0+'.

    After Compline,
    Zaxo

Re: Using overload. Any complete (simple) examples?
by tilly (Archbishop) on Oct 08, 2003 at 03:08 UTC
Re: Using overload. Any complete (simple) examples?
by tsee (Curate) on Oct 08, 2003 at 13:33 UTC
    As for overloading 0+/numeric context, it seemed to work fine when I started using overload for the interface to Math::Symbolic. In stringification context, $obj->to_string('infix') is called, in numeric context (and boolean, too), $obj->value() is called which returns the value of the function or undef if not all variables have a value assigned to them.
    If you're interested, have a look at the Math::Symbolic::Base package which has the overloading code and which is, well, the base class for ::Operator, ::Variable, and ::Constant classes/objects.

    The overloading doesn't include [] and friends because I couldn't see a good use for that. :) But I suppose the arithmetic overloading is ideal for a thing like symbolic mathematics: $obj + 2 resulting in a new Math::Symbolic tree. Unlike the Big* stuff, the code is all Perl. I like to think of it being fairly well documented, too.

    http://search.cpan.org/~smueller/Math-Symbolic/

    Steffen
Symbolic calculations with operator overload
by ambrus (Abbot) on Oct 09, 2003 at 06:21 UTC
    Here's some code I wrote last summer (and changerd a bit now).
    #!/usr/bin/perl -w # # We will do operations with multi-variable real polinomials and 3d ve +ctors # of these. These are slow algorithms, but might be good examples for # object orientation ond operator overloading in Perl. It is also not # commented well. Many functions are defined but not used, altough I +have # tested them. It is not difficult to add some calculations that use # these functions. #

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://297372]
Approved by diotalevi
Front-paged by diotalevi
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-07-22 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (453 votes). Check out past polls.

    Notices?