Shell-Base
view release on metacpan or search on metacpan
close $P;
}
Note the explicit use of CORE::print, to prevent infinite recursion.
=item parseline
A line is divided into ($command, %env, @args) using
$self->parseline(). A command C<foo> is dispatched to a method
C<do_foo>, with @args passed as an array, and with %ENV updated to
include %env.
If there is no C<do_foo> method for a command C<foo>, then the method
C<default> will be called. Subclasses can override the C<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
F</bin/sh> does; the command:
FOO=bar baz
Invokes the C<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.
=item 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 C<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, C<%u@%h %w %%>,
which might yield a prompt like:
darren@tumbleweed /tmp/Shell-Base %
(See L<String::Format> for the appropriate details.)
The value passed to C<prompt> can be a code ref; if so, it is invoked
with $self and any additional arguments passed to C<prompt> as the
arguments:
$self->prompt(\&func, @stuff);
Will call:
&$func($self, @stuff);
and use the return value as the prompt string.
=item intro / outro
Text that is displayed when control enters C<run> (C<intro>) and
C<quit> (C<outro>). If the method returns a non-undef result, it will
be passed to $self->print().
=item quit
The C<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.
=back
=head2 Methods That Add Commands
Any command that run() doesn't recognize will be treated as a command;
a method named C<do_$command> will be invoked, in an eval block.
Remember that a line is parsed into ($command, %env, @args);
C<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 C<env> command:
( run in 0.877 second using v1.01-cache-2.11-cpan-df04353d9ac )