Help with overload.

Discussion in 'Perl Misc' started by Ignoramus27279, Jul 29, 2005.

  1. 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
    Ignoramus27279, Jul 29, 2005
    #1
    1. Advertising

  2. Ignoramus27279

    Paul Lalli Guest

    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
    Paul Lalli, Jul 29, 2005
    #2
    1. Advertising

  3. On 29 Jul 2005 12:37:48 -0700, Paul Lalli <> wrote:
    > 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...


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

    i

    > perldoc overload
    >
    > Paul Lalli
    >



    --
    Ignoramus27279, Jul 29, 2005
    #3
  4. Ignoramus27279

    Anno Siegel Guest

    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.
    Anno Siegel, Jul 30, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    361
    Nick Hounsome
    Jan 25, 2004
  2. Jason
    Replies:
    5
    Views:
    542
    John Harrison
    Feb 12, 2004
  3. Stephen Antes

    Filestream/operater overload help

    Stephen Antes, Sep 21, 2004, in forum: C++
    Replies:
    1
    Views:
    355
    John Harrison
    Sep 21, 2004
  4. dragon
    Replies:
    5
    Views:
    383
    dragon
    Dec 22, 2005
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    236
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page