Help with overload.

I

Ignoramus27279

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
 
P

Paul Lalli

Ignoramus27279 said:
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
 
A

Anno Siegel

Ignoramus27279 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top