Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Basic Objects with Overloaded Operators

by perlmonkey (Hermit)
on Apr 20, 2000 at 00:32 UTC ( #8076=perltutorial: print w/ replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    package Complex;
    use Carp;
    
    ...
            return $self->[$position] = $value;
        }
    }
    
  2. or download this
    use Complex;
    #create a new object
    my $x = new Complex;
    ...
    $x->Imag(-5);                #set $x's imaginary part to '-5'
    
    print $x->Real." ".$y->Imag; #prints '3 -3'
    
  3. or download this
    use overload
        "\"\"" => \&Cmp_string,
        "+"    => \&Cmp_add,
        "*"    => \&Cmp_multiply;
    
  4. or download this
        print $x;             #prints '3 - 5I'
        my $str = "$x";       #$str = '3 - 5I'
    
  5. or download this
        $x = $x + $y;
        $y = $x + 2;
        $y =  2 + $x;
    
  6. or download this
        $y = $x * $x;
        $y = $x * 2;
        $y =  2 * $x;
    
  7. or download this
    sub Cmp_string
    {
        #get the object
    ...
        #   arithmetic    
        return new Complex($real, $imag);
    }
    
  8. or download this
    package Complex;
    use Carp;
    use strict;
    ...
    
    #module exit status
    1;
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2015-07-31 12:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls