Shell-Base
view release on metacpan or search on metacpan
open my $P, "|$pager" or carp "Can't open $pager: $!";
CORE::print $P @stuff;
close $P;
}
Note the explicit use of CORE::print, to prevent infinite recursion.
parseline
A line is divided into ($command, %env, @args) using
$self->parseline(). A command "foo" is dispatched to a method
"do_foo", with @args passed as an array, and with %ENV updated to
include %env.
If there is no "do_foo" method for a command "foo", then the method
"default" will be called. Subclasses can override the "default"
method.
%ENV is localized and updated with the contents of %env for the
current command. %env is populated in a similar fashion to how
/bin/sh does; the command:
FOO=bar baz
Invokes the "do_baz" method with $ENV{'FOO'} = "bar".
Shell::Base doesn't (currently) do anything interesting with
pipelines; the command:
foo | bar
will be parsed by parseline() as:
("foo", {}, "|", "bar")
rather than as two separate connected commands. Support for
pipelines in on the TODO list.
prompt
Gets or sets the current prompt. The default prompt is:
sprintf "(%s) \$ ", __PACKAGE__;
The prompt method can be overridden, of course, possibly using
something like "String::Format":
use Cwd;
use File::Basename qw(basename);
use Net::Domain qw(hostfqdn);
use String::Format qw(stringf);
use Sys::Hostname qw(hostname);
sub prompt {
my $self = shift;
my $fmt = $self->{ PROMPT_FMT };
return stringf $fmt => {
'$' => $$,
'w' => cwd,
'W' => basename(cwd),
'0' => $self->progname,
'!' => $self->prompt_no,
'u' => scalar getpwuid($<),
'g' => scalar getgrgid($(),
'c' => ref($self),
'h' => hostname,
'H' => hostfqdn,
};
}
Then $self->{ PROMPT_FMT } can be set to, for example, "%u@%h %w
%%", which might yield a prompt like:
darren@tumbleweed /tmp/Shell-Base %
(See String::Format for the appropriate details.)
The value passed to "prompt" can be a code ref; if so, it is invoked
with $self and any additional arguments passed to "prompt" as the
arguments:
$self->prompt(\&func, @stuff);
Will call:
&$func($self, @stuff);
and use the return value as the prompt string.
intro / outro
Text that is displayed when control enters "run" ("intro") and
"quit" ("outro"). If the method returns a non-undef result, it will
be passed to $self->print().
quit
The "quit" method currently handles closing the history file; if it
is overridden, $self->SUPER::quit() should be called, so that the
history file will be written out.
The results of $self->outro() will be passed to $self->print() as
well.
Methods That Add Commands
Any command that run() doesn't recognize will be treated as a command; a
method named "do_$command" will be invoked, in an eval block. Remember
that a line is parsed into ($command, %env, @args); "do_$command" will
be invoked with @args as @_, and %ENV updated to include the contents of
%env. The effect is similar to:
my ($command, $env, @args) = $self->parseline($line);
my $method = "do_$command";
local %ENV = (%ENV, %$env);
my $output = $self->$method(@args);
$output will be passed to $self->print() if it is defined.
Here is method that implements the "env" command:
sub do_env {
my ($self, @args) = @_;
my @output;
( run in 0.485 second using v1.01-cache-2.11-cpan-df04353d9ac )