Cmd::Interpreter - Support for line-oriented command interpreters
use Cmd::Interpreter; our @ISA = qw(Cmd::Interpreter);
Cmd::Interpreter provides a simple framework for writing line-oriented command interpreters.
package Example::Hello; use strict; use warnings; use Cmd::Interpreter; our @ISA = qw(Cmd::Interpreter); sub help { my $self = shift; print "common help\n"; return ''; } sub do_hello { my $self = shift; print "Hello " . (shift || "World") . "!\n"; return ''; } sub help_hello { my $self = shift; print "help for hello\n"; return ''; } sub do_quit { my $self = shift; print "By\n"; return "quit"; } sub empty_line { } 1;
#!/usr/bin/env perl use strict; use warnings; use Example::Hello; my $ex = Example::Hello->new(prompt => 'example> '); $ex->run("Welcome to hello world app.");
You can pass program name as prog_name, prompt as prompt.
prog_name
prompt
Loop stoping if function returns true value aka stop flag.
stop flag
Will execute on command 'foo'.
Will execute on command '?foo' or 'help foo'.
Will execute when input is '?' or 'help'.
Will execute before loop.
Will execute after loop.
Receive input line, return one (can be changed).
Receive stop flag, line (from pre_cmd). Return stop flag.
Will execute when input command not exists.
Will execute when input defined but empty. By default execute last command if one exists.
Will execute when input undefined.
Will execute when input is '!cmd [args]' or 'shell cmd [args]'.
Command history works fine with such module like Term::ReadLine::Perl.
You can install Cmd::Interpreter from cpanm git@github.com:oakulikov/Cmd-Interpreter.git.
cpanm git@github.com:oakulikov/Cmd-Interpreter.git
Oleg Kulikov <oakulikov@yandex.ru>
Authors of Python Lib/cmd.py
Copyright (C) Oleg Kulikov.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Cmd::Interpreter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Cmd::Interpreter
CPAN shell
perl -MCPAN -e shell install Cmd::Interpreter
For more information on module installation, please visit the detailed CPAN module installation guide.