Overloaded operators: Difference between revisions
m
→{{header|Perl}}: revise code layout to emphasize parallelism
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: revise code layout to emphasize parallelism) |
||
Line 458:
so that they can be used on members of that class(package). Also see 'Zeckendorf arithmetic' where overloading
is used on Zeckendorf numbers.
<syntaxhighlight lang="perl">
my $x = Ones->new( 15 );▼
print "$x + $y = $z\n";▼
print "$x - $y = $z\n";▼
package Ones;
use overload qw("" asstring + add - subtract * multiply / divide);
sub new ( $class, $value ) { bless \('1' x $value), ref $class || $class }
sub asstring ($self, $other, $) { $$self }
sub asdecimal ($self, $other, $) { length $$self }
sub add ($self, $other, $) { bless \(
▲ bless \($$self =~ s/$$other//r), ref $self;
package main;
▲ bless \($$self =~ s/1/$$other/gr), ref $self;
$z =
▲ $self->new( $$self =~ s/$$other/$$other/g );
$z =
{{out}}
<pre>
|