Debian Codegraph @

examples/tui-sample

raw
1 #!/usr/bin/perl -w
2 # tui-sample Scott Bronson 16 Dec 2003
3
4 # This shows the similarities between Term::TUI (see the sample.pl file)
5 # and Term::ShellUI. This example allows you to enter any number of arguments
6 # (except for subs of course).
7
8 # This file is released under the MIT license.
9
10 use strict;
11 use lib '../lib';
12 use Term::ShellUI;
13
14 my $term = new Term::ShellUI(commands => get_commands());
15 $term->run();
16
17 sub get_commands
18 {
19 return {
20 "abort" => { alias => "quit" },
21 "help" => { desc => "Print helpful information",
22 args => sub { shift->help_args(undef, @_); },
23 method => sub { shift->help_call(undef, @_); } },
24 "quit" => { desc => "Quit using this program",
25 maxargs => 0, method => sub { shift->exit_requested(1); } },
26 "math" => {
27 desc => "A simple calculator",
28 cmds => {
29 "add" => {
30 args => "(any number of decimal numbers)",
31 desc => "Add numbers together",
32 proc => sub { print "Total=" . add(@_) . "\n" }, },
33 "mult" => {
34 args => "(any number of decimal numbers)",
35 desc => "Multiply numbers together",
36 proc => sub { print "Total=" . mult(@_) . "\n" }, },
37 "hex" => {
38 desc => "Do math in hex",
39 cmds => {
40 "add" => {
41 args => "(any number of hexadecimal numbers)",
42 desc => "Add numbers together, result in hex",
43 proc => sub { print "Total=" . ashex(\&add,@_) . "\n" }, },
44 "mult" => {
45 args => "(any number of hexadecimal numbers)",
46 desc => "Multiply numbers together, result in hex",
47 proc => sub { print "Total=" . ashex(\&mult,@_) . "\n" }, },
48 },
49 },
50 },
51 },
52 "string" => {
53 desc => "String operations",
54 cmds => {
55 "subs" => {
56 args => ["(string)", "(pos)", "(len)"],
57 desc => "Take STRING,POS,LEN and return a substring.",
58 minargs => 1, maxargs => 3,
59 proc => sub { print "Substring=".substr(shift,shift||0,shift||0)."\n" }, },
60 "len" => {
61 args => "(any number of strings)",
62 desc => "Calculate length of arguments",
63 proc => sub { print "Length=" . join(", ", map { length } @_) . "\n" }, },
64 },
65 },
66 };
67 }
68
69 sub add {
70 my $sum=0;
71 for(@_) { $sum+=$_; }
72 return $sum;
73 }
74
75 sub mult {
76 my $prod=1;
77 for(@_) { $prod*=$_; }
78 return $prod;
79 }
80
81 sub ashex {
82 my $sub = shift;
83 return sprintf("%x", &$sub(map { hex } @_));
84 }
85