Last active
March 25, 2021 22:24
-
-
Save cognominal/5228672 to your computer and use it in GitHub Desktop.
trying to implement a class that is both an hash and an array, I have problem to access an element of an array using a .[]
The method I declared is not used.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DOM { | |
has @!kids handles <shift unshift push pop elems>; | |
has %!attrs; | |
method kids-delete(*@indices) { @!kids.delete(@indices) } | |
=begin pod | |
A class may define methods that allow it to respond as if it were a | |
routine, array, or hash. The long forms are as follows: | |
method postcircumfix:<( )> (|capture) {...} | |
method postcircumfix:<[ ]> (**@slice) {...} | |
method postcircumfix:<{ }> (**@slice) {...} | |
Those are a bit unwieldy, so you may also use these short forms: | |
method &.( |capture ) {...} | |
method @.[ **@slice ] {...} | |
method %.{ **@slice } {...} | |
=end pod | |
# cargo cult. | |
method postcircumfix:sym<[ ]>(**@slice) { say "help!"; @!kids[@slice] } | |
} | |
my $d = DOM.new; | |
print "$d.elems() "; | |
$d.push: 'hi!'; | |
$d.push: 'bye!'; | |
$d.kids-delete(0); # ok | |
print "$d.elems() "; | |
say $d[0]; # does not work. prints "DOM.new()" | |
$d.pop; | |
say "$d.elems() "; | |
=begin END | |
0 2 DOM.new() | |
1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment