-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlexical-method.pl
42 lines (35 loc) · 926 Bytes
/
lexical-method.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env perl
# https://metacpan.org/release/HYDAHY/perl-5.41.9/changes
# https://metacpan.org/release/HYDAHY/perl-5.41.9/view/pod/perlclass.pod
use v5.41;
use experimental 'class';
class Calc {
my method process($self, $x, $y, $op) {
if ($op eq '+') {
return $x + $y;
}
elsif ($op eq '-') {
return $x - $y;
}
elsif ($op eq '*') {
return $x * $y;
}
else {
return;
}
}
method plus($x, $y) {
$self->&process($x, $y, '+');
}
method minus($x, $y) {
$self->&process($x, $y, '-');
}
method multiply($x, $y) {
$self->&process($x, $y, '*');
}
}
my $calc = Calc->new;
say $calc->plus(3,2); # 5
say $calc->minus(3,2); # 1
say $calc->multiply(3,2); # 6
say $calc->process(3,2,'+'); # Can't locate object method "process" via package "Calc"