Skip to content

Instantly share code, notes, and snippets.

@softmoth
Created January 2, 2012 23:57
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 softmoth/1552694 to your computer and use it in GitHub Desktop.
Save softmoth/1552694 to your computer and use it in GitHub Desktop.
BUILD() method called after object creation? how in perl6?
#! /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