Skip to content

Instantly share code, notes, and snippets.

@cognominal
Last active March 25, 2021 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cognominal/5228672 to your computer and use it in GitHub Desktop.
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.
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