For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > Help with overload.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help with overload.
Ignoramus27279

2005-07-29, 5:03 pm

Just a pointer to a perl manpage would be great.

I am looking to write a bunch of modules on which to perform
operations like addition etc.

The modules would represent objects such as constants (1, 2/3, pi/2,
etc), as well as polynomials and general math functions.

What I would like is to overload operations so that I could "drop in"
the values of that type into existing subroutines.

Suppose that I already have a subroutine

sub myGreatComputation {
my ($a, $b) = @_.
# do big stuff with arithmetics on $a and $b
}

is there some way to define my new module so that other subroutines
that do arithmetics would work properly when passed objects of that
module rather than regular scalar values, without rewriting that
subroutine?

thanks


Paul Lalli

2005-07-29, 5:03 pm

Ignoramus27279 wrote:
> Subject: Help with overload.
> Just a pointer to a perl manpage would be great.


I have to wonder what you tried first, before asking hundreds of people
around the world...

perldoc overload

Paul Lalli

Ignoramus27279

2005-07-29, 5:04 pm


On 29 Jul 2005 12:37:48 -0700, Paul Lalli <mritty@gmail.com> wrote:
> Ignoramus27279 wrote:
>
> I have to wonder what you tried first, before asking hundreds of people
> around the world...


bingo, that's what I was looking for. thanks.

i

> perldoc overload
>
> Paul Lalli
>



--

Anno Siegel

2005-07-30, 5:00 pm

Ignoramus27279 <ignoramus27279@NOSPAM.27279.invalid> wrote in comp.lang.perl.misc:
> Just a pointer to a perl manpage would be great.


You have been pointed to the overload man page.

> I am looking to write a bunch of modules on which to perform
> operations like addition etc.
>
> The modules would represent objects such as constants (1, 2/3, pi/2,
> etc), as well as polynomials and general math functions.


Oh. Good luck!

> What I would like is to overload operations so that I could "drop in"
> the values of that type into existing subroutines.


Well, that's what overloading is for.

> Suppose that I already have a subroutine
>
> sub myGreatComputation {
> my ($a, $b) = @_.
> # do big stuff with arithmetics on $a and $b
> }


Exactly, that's how it is supposed to work.

> is there some way to define my new module so that other subroutines
> that do arithmetics would work properly when passed objects of that
> module rather than regular scalar values, without rewriting that
> subroutine?


The class ModArith below implements modular integer arithmetic using
overload. While that's a little less ambitious than your project, the
basic structure is the same. The "general-purpose" routine factorial()
calculates plain or modular factorials, depending only on the nature
of the input. Since the class ModArith doesn't implement subtraction,
factorial() is written a bit funny ("$n = $n + -1" instead of "-- $n"),
but that's a deficiency of the example, not of the general principle.


my @elem = map ModArith->new( $_), 0 .. 6;

print "Addition:\n";
for my $l ( @elem ) {
my @sums = map $l + $_, @elem;
print "@sums\n";
}

print "\nMultiplication:\n";
for my $l ( @elem ) {
my @prods = map $l * $_, @elem;
print "@prods\n";
}

print "\nFactorial:\n";
for ( 1 .. 5 ) {
my $fac = factorial( $_);
my $modfac = factorial( ModArith->new( $_));
print "$_ : $modfac ($fac)\n";
}

sub factorial {
my $n = shift;
my $res = 1;
while ( $n ) {
$res *= $n;
$n = $n + -1; # in deference to incomplete implementation
}
$res;
}

########################################
################################

package ModArith;
use constant MODULUS => 7;

use overload (
'""' => 'value',
'bool' => 'value',
'+' => 'add',
'*' => 'multiply',
);

sub new {
my ( $class, $num) = @_;
bless \ $num, $class;
}

sub value { ${ $_[ 0]} % MODULUS } # subclass and override to change modulus

sub add {
my ( $self, $other) = @_;
my $op = ref $other ? $other->value : $other;
ref( $self)->new( $self->value + $op);
}

sub multiply {
my ( $self, $other) = @_;
my $op = ref $other ? $other->value : $other;
ref( $self)->new( $self->value * $op);
}

__END__

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com