File: vend_interactive.pl

package info (click to toggle)
libur-perl 0.470%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,192 kB
  • sloc: perl: 61,814; javascript: 255; xml: 108; sh: 13; makefile: 9
file content (83 lines) | stat: -rwxr-xr-x 1,751 bytes parent folder | download | duplicates (4)
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
#!/gsc/bin/perl

use strict;
use warnings;
use above 'Vending';

my $machine = Vending::Machine->get();
unless ($machine) {
    print STDERR "Out of order...\n";
    exit;
}

my %command_map = (
    help => \&help,
    done => \&done,
    'check-again' => \&clear_query_cache,
    'coin-return' => 'Vending::Command::CoinReturn',
    dollar => 'Vending::Command::Dollar',
    quarter => 'Vending::Command::Quarter',
    dime => 'Vending::Command::Dime',
    nickel => 'Vending::Command::Nickel',
    buy => 'Vending::Command::Buy',
    menu => 'Vending::Command::Menu',
);

$|=1;
&help();
while (1) {
    print "command> ";
    my $line = <>;
    last unless $line;
    chomp($line);

    my @words = split(/\s+/, $line);

    my $thing = $command_map{shift @words};
    if (ref($thing)) {
        # It's a sub we can just call
        $thing->();
    } elsif($thing) {
        # It's a command class name
        my $command = $thing->create(bare_args => \@words);
        if ($command->execute() ) {
            UR::Context->commit();
        }
    } else {
        print "That is not a valid command\n";
    }

}

&done();


sub done {
    print "\nGoodbye\n";
    exit(0);
}

sub help {
    print q(
Vendco Vending Machine available commands:
dollar - insert a dollar
quarter - insert a quarter
dime - insert a dime
nickel - insert a nickel
menu - See what is available
buy <slot> - purchase an item from the menu
coin-return - return any coins you inserted
help - this help text
check-again - secret backdoor to use when another progrtam reloads the inventory

);

}

sub clear_query_cache {
    print "Forgetting about Vending::Merchandises and Vending::Coins\n";
    Vending::Merchandise->unload();
    Vending::Coin::Change->unload();
}