Perl code en verhaaltjes er omheen

A bedtime story for our little geeks en

By merlijn85 on Wednesday 26 August 2009 13:04 - Comments (4)
Category: Perl, Views: 2.818

We cannot start early enough to teach our children the most valuable skill in life: Programming. Hence I've spent a little time writing code that should be easy to relate to for kids.

So for those of us that have children, you can now share the glory of perl through a story that's been read to kids for centuries:


code:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/perl

package Little_Red_Riding_Hood;

sub new {
    ($class,$item,$dest,$route) = @_;
    $lrrh = {
        hooded_cape => 'red',
        name => 'Little Red Riding Hood',
        item => $item,
        destination => $dest,
        route => $route
    };
    bless $lrrh, $class;
}

sub walk_to_dest {
    $self = shift;
    print 'Little Red Riding Hood: I\'m going to deliver ' . $self->{item} . ' to my ' . $self->{destination} . ' by walking through ' . $self->{route} . ".\n";
}

sub respond_to_wolf {
    my $self = shift;
    print 'Little Red Riding Hood: Hello Mr Wolf, I\'m going to deliver ' . $self->{item} . ' to my ' . $self->{destination} . ".\n";
}

sub pick_flowers {
    print "Little Red Riding Hood: Good idea, I will do so right away!\n";
    $_[0]->{flowers} = 1;
}

sub approach_grandma {
    print "Little Red Riding Hood: Hello Grandma - My, what big teeth you have!\n";
}

sub get_eaten {
    print "Little Red Riding Hood: GULP!\n";
}

package Wolf;

sub new {
    ($class,$goal) = @_;
    $wolf = {
        goal => $goal
    };
    bless $wolf, $class;
}

sub approach_lrrh {
    $self = shift;
    print 'Wolf: I would like to ' . $self->{goal} . " but it is too public here, where are you going?\n";
}

sub suggest_flowers {
    print "Wolf: You should pick some flowers!\n";
}

sub goto_grandma {
    print "Wolf: Hello grandma, it's me - Little Red Riding Hood.\n";
}

sub eat {
    ($self,$victim) = @_;
    print 'Wolf: I\'m going to eat you now, ' . $victim->{name} . ".\n";
    $victim->get_eaten;
}

sub dress_up_as_grandma {
    print "Wolf: I'm going to dress up like grandma to trick Little Red Riding Hood!\n";
}

sub respond_lrrh {
    print "Wolf: The better to eat you with!\n";
}

package Grandma;

sub new {
    ($class,$name) = @_;
    $grandma = {
        name => $name
    };
    bless $grandma, $class;
}

sub let_wolf_in {
    print "Grandma: Come on in!\n";
}

sub get_eaten {
    print "Grandma: GULP!\n";
}

package main;

$lrrh = new Little_Red_Riding_Hood('food', 'sick grandma', 'woods');
$lrrh->walk_to_dest();

$wolf = new Wolf('eat Little Red Riding Hood');
$wolf->approach_lrrh;
$lrrh->respond_to_wolf;
$wolf->suggest_flowers;
$lrrh->pick_flowers;

$grandma = new Grandma('grandma');
$wolf->goto_grandma;
$grandma->let_wolf_in;
$wolf->eat($grandma);
$wolf->dress_up_as_grandma;

$lrrh->approach_grandma;
$wolf->respond_lrrh;
$wolf->eat($lrrh);



Download

Sleep tight and don't let the bed bugs bite!