Commit | Line | Data |
d93106d7 |
1 | #!perl |
2 | use strict; |
3 | use warnings FATAL => 'all'; |
4 | |
5 | use Test::More tests => 3; |
6 | |
7 | # testing that we can install several different keywords into the same scope |
8 | { |
9 | package Monster; |
10 | |
11 | use Function::Parameters; |
12 | use Function::Parameters { |
f7651a6e |
13 | action => { defaults => 'method', shift => '$monster' }, |
14 | constructor => { defaults => 'method', shift => '$species' }, |
1a52f2db |
15 | function => 'function', |
d93106d7 |
16 | }; |
17 | |
18 | constructor spawn { |
19 | bless {@_}, $species; |
20 | } |
21 | |
22 | action speak (@words) { |
23 | return join ' ', $monster->{name}, $monster->{voices}, @words; |
24 | } |
25 | |
26 | action attack ($me: $you) { |
27 | $you->take_damage($me->{strength}); |
28 | } |
29 | |
30 | method take_damage ($hits) { |
31 | $self->{hitpoints} = calculate_damage($self->{hitpoints}, $hits); |
32 | if($self->{hitpoints} <= 0) { |
33 | $self->{is_dead} = 1; |
34 | } |
35 | } |
36 | |
37 | function calculate_damage ($hitpoints, $damage) { |
38 | return $hitpoints - $damage; |
39 | } |
40 | } |
41 | |
42 | package main; |
43 | my $hellhound = Monster->spawn( name => "Hellhound", voices => "barks", strength => 22, hitpoints => 100 ); |
44 | is $hellhound->speak(qw(arf arf)), 'Hellhound barks arf arf'; |
45 | |
46 | my $human = Monster->spawn( name => 'human', voices => 'whispers', strength => 4, hitpoints => 16 ); |
47 | $hellhound->attack($human); |
48 | is $human->{is_dead}, 1; |
49 | is $human->{hitpoints}, -6; |
50 | |