Term::Shell is excellent scaffolding and support when you do not want to muck around with Term::Readline yourself. As long as you have one of the Term::Readline:: alternatives installed, you get command-line editing and history for free.
A demonstration:
knan@viconia:~$ perl ./listing3.pl
World domination. But first, take care of the spam.
spsh:/> ls
No spam. Yet.
spsh:/> ?
Unknown command '?'; type 'help' for a list.
spsh:/> help
Type 'help command' for more detailed help.
Commands:
exit - exits the program
help - prints this screen, or help on 'command'
ls - Is there any spam? - no help available
spsh:/> exit
So far so good. Add a help_ls subroutine if you feel like explaining the intricacies of spam listing to your fellow spam handlers.
Source of the shell above:
package main;
Spsh->new->cmdloop;
package Spsh;
use base qw(Term::Shell);
sub preloop
{
$state_path = "";
binmode(STDOUT, ":utf8");
print "World domination. But first,"
."take care of the spam.\n";
}
sub prompt_str
{
return "spsh:/$state_path> ";
}
# Define an empty command, to avoid
# "unknown command" on an empty
# command-line.
sub run_
{
return;
}
sub run_ls
{
print "No spam. Yet.\n";
return;
}
sub smry_ls
{
return "Is there any spam?"
}
No comments:
Post a Comment