Created
January 2, 2012 23:57
-
-
Save softmoth/1552694 to your computer and use it in GitHub Desktop.
BUILD() method called after object creation? how in perl6?
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
#! /usr/bin/env perl | |
use strict; | |
use warnings; | |
package Vocab; | |
use Moose; | |
my %WORDS; | |
has 'word' => ( is => 'ro', required => 1, isa => 'Str' ); | |
has 'synonyms' => ( is => 'rw', isa => 'ArrayRef[Str]', default => sub { [] } ); | |
sub BUILD { | |
my $self = shift; | |
my @params = @_; | |
foreach my $word ($self->{word}, @{$self->{synonyms}}) { | |
$WORDS{$word} ||= []; | |
push @{$WORDS{$word}}, $self; | |
} | |
} | |
sub dump { | |
use Data::Dumper; | |
print STDERR Dumper(\%WORDS); | |
} | |
package Noun; | |
use Moose; | |
extends 'Vocab'; | |
package Verb; | |
use Moose; | |
extends 'Vocab'; | |
package main; | |
use Data::Dumper; | |
Noun->new({word => 'fire', synonyms => ['flame']}); | |
Verb->new({word => 'fire', synonyms => ['launch', 'shoot']}); | |
Verb->new({word => 'fire', synonyms => ['terminate']}); | |
Vocab->dump(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment