File: 03-config.t

package info (click to toggle)
libfunction-parameters-perl 2.001003-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 948 kB
  • sloc: perl: 6,478; makefile: 3
file content (50 lines) | stat: -rw-r--r-- 1,332 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!perl
use strict;
use warnings FATAL => 'all';

use Test::More tests => 3;

# testing that we can install several different keywords into the same scope
{
    package Monster;

    use Function::Parameters;
    use Function::Parameters {
        action => { defaults => 'method', shift => '$monster' },
        constructor => { defaults => 'method', shift => '$species' },
        function => 'function',
    };

    constructor spawn (@) {
        bless {@_}, $species;
    }

    action speak (@words) {
        return join ' ', $monster->{name}, $monster->{voices}, @words;
    }

    action attack ($me: $you) {
        $you->take_damage($me->{strength});
    }

    method take_damage ($hits) {
        $self->{hitpoints} = calculate_damage($self->{hitpoints}, $hits);
        if($self->{hitpoints} <= 0) {
            $self->{is_dead} = 1;
        }
    }

    function calculate_damage ($hitpoints, $damage) {
        return $hitpoints - $damage;
    }
}

package main;
my $hellhound = Monster->spawn( name => "Hellhound", voices => "barks", strength => 22, hitpoints => 100 );
is $hellhound->speak(qw(arf arf)), 'Hellhound barks arf arf';

my $human = Monster->spawn( name => 'human', voices => 'whispers', strength => 4, hitpoints => 16 );
$hellhound->attack($human);
is $human->{is_dead}, 1;
is $human->{hitpoints}, -6;