Skip to content

Commit a62eedb

Browse files
committedMay 25, 2022
- Added mro.
1 parent 3dc0103 commit a62eedb

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
##### [Line ending](/v510/line-ending.pl)
2323
##### [Delete hash](/v510/delete-hash.pl)
2424
##### [Defined-or](/v510/defined-or.pl)
25+
##### [Method Resolution Order (dfs)](/v510/mro-dfs.pl)
26+
##### [Method Resolution Order (c3)](/v510/mro-c3.pl)
2527

2628
## Perl v5.12
2729
##### [Each array](/v512/each-array.pl)

‎v510/mro-c3.pl

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/perl
2+
3+
package ROOT { sub new {} }
4+
package P12 { use base qw(ROOT) }
5+
package P11 { use base qw(P12) }
6+
package P22 { use base qw(ROOT) }
7+
package P21 { use base qw(P22) }
8+
package P1 { use base qw(P11 P21) }
9+
package P2 { use base qw(P21 P11) }
10+
11+
package main {
12+
use strict; use warnings;
13+
14+
use mro;
15+
use v5.10;
16+
use Test::More;;
17+
18+
# method resolution order (dfs)
19+
is_deeply mro::get_linear_isa('P1'),
20+
[qw(P1 P11 P12 ROOT P21 P22)];
21+
22+
is_deeply mro::get_linear_isa('P2'),
23+
[qw(P2 P21 P22 ROOT P11 P12)];
24+
25+
# method resolution order (c3)
26+
is_deeply mro::get_linear_isa('P1', 'c3'),
27+
[qw(P1 P11 P12 P21 P22 ROOT)];
28+
29+
is_deeply mro::get_linear_isa('P2', 'c3'),
30+
[qw(P2 P21 P22 P11 P12 ROOT)];
31+
32+
done_testing;
33+
}

‎v510/mro-dfs.pl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/perl
2+
3+
package P12 { sub new {} }
4+
package P11 { use base qw(P12) }
5+
package P22 { sub new {} }
6+
package P21 { use base qw(P22) }
7+
package P1 { use base qw(P11 P21) }
8+
package P2 { use base qw(P21 P11) }
9+
10+
package main {
11+
use strict; use warnings;
12+
13+
use mro;
14+
use v5.10;
15+
use Test::More;;
16+
17+
# method resolution order (dfs)
18+
is_deeply mro::get_linear_isa('P1'), [qw(P1 P11 P12 P21 P22)];
19+
is_deeply mro::get_linear_isa('P2'), [qw(P2 P21 P22 P11 P12)];
20+
21+
done_testing;
22+
}

0 commit comments

Comments
 (0)
Please sign in to comment.