Markus Laire writes: > [EMAIL PROTECTED] kirjoitti: > >Please, > >I have a question if exists in Perl somethink like keyword > >'operator' in C++ ? > > That will exist in perl6.
And to quite a larger extent. Not only can you overload existing operators, you can make up whatever operator name you like. > >for example we can write in C++ : > >class A { > >A() { printf("Constructor of object class A\n"); } > >~A() { printf("Destructor of object class A\n"); } > >}; > >A &operator + (A &a1, A &a2) { printf("Addition\n"); } > >A &operator * (A &a1, A &a2) { printf("Multiplication\n"); } > > > > int main() { > > A a,b,c; > > c = (a+b*a); > > } > > Using these as reference: > > http://dev.perl.org/perl6/synopsis/S06.html > http://dev.perl.org/perl6/synopsis/S12.html > > (I don't understand how to create constructor & destructor) > I think equivalent perl6-code would be: > > class A { > # place constructor here submethod BUILD() { say "Constructor of class A"; } > # place destructor here submethod DESTROY() { say "Destructor of class A"; } > } > > sub infix:<+> (A $a1, A $a2) { print("Addition\n"); } > sub infix:<*> (A $a1, A $a2) { print("Multiplication\n"); } I expect you need "multi" on those subs, to avoid redefining the existing operators (not without it yelling at you a bit, though). multi sub infix:<+> (A $a1, A $a2) { say "Addition" } multi sub infix:<*> (A $a1, A $a2) { say "Multiplication" } > my A $a; # note, I'm not sure how to write this on one line > my A $b; > my A $c; Just: my A ($a, $b, $c); Luke > $c = ($a + $b * $a);