Term::ShellKit::Commands - Basic shell functions


Term-ShellKit documentation Contained in the Term-ShellKit distribution.

Index


Code Index:

NAME

Top

Term::ShellKit::Commands - Basic shell functions

SYNOPSIS

Top

  > perl -MTerm::ShellKit -eshell
  Term::ShellKit: Starting interactive shell

  Term::ShellKit> eval join ', ', 1 .. 10 
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10

  Term::ShellKit> alias incr eval $i += 1
  Term::ShellKit> incr
  1
  Term::ShellKit> incr
  2

  Term::ShellKit> help
  NAME
      Term::ShellKit - Generic Perl command-line environment
  ...

  Term::ShellKit> exit




COMMANDS

Top

The following commands are available.

alias

Manipulates Term::ShellKit aliases.

echo (alias ")

Prints the provided argument.

eval (alias $)

Run some Perl using eval.

execute (alias !)

Run an external program with Perl's qx operator.

exit (alias quit, or q)

Exit the shell.

help (alias ?)

Uses the pod2text script to display documentation for a Perl module.

kit

Load a ShellKit package.

Try "kit Dev" to load the Dev kit, or "help kit Dev" to learn more about it.

Typing "kit AutoPager" will provide ShellKit with output buffered through /usr/bin/more or your prefered pager program.

SEE ALSO

Top

Term::ShellKit


Term-ShellKit documentation Contained in the Term-ShellKit distribution.

package Term::ShellKit::Commands;

require Term::ShellKit;

######################################################################

sub eval ($) {
  my $input = shift;
  my @results = eval "package $Term::ShellKit::CurrentPackage; no strict qw( refs vars subs ); $input";
  die $@ if $@;
  ! scalar( @results ) ? '' : 
  scalar( @results == 1 ) ? (
      ( ! defined $results[0] ) ? 'undef' : "$results[0]"
    ) : 
      join(', ', map { 
      ( ! defined ) ? 'undef' : ( ref $_ ) ? "$_" : "'$_'" 
    } @results );
}

sub package ($) {
  my $input = shift;
  $input =~ s/\;\s*$//;
  $Term::ShellKit::CurrentPackage = $input;
}

sub exit {
  exit;
}

sub kit { 
  die "No kit name provided" unless ( scalar @_ );
  map { Term::ShellKit::load_kit( $_ ) } @_; 
}

######################################################################

sub execute ($) {
  my $command = shift;
  qx{$command};
}

######################################################################

sub echo (;$) {
  shift;
}

######################################################################

use vars qw( $PODViewer );
$PODViewer ||= "Pod::Text" || "Pod::Text::TermCap";

sub help (;$) {
  my $topic = shift || 'Term::ShellKit::Commands';
  
  $topic =~ s/kit /Term::ShellKit::/;
  if ( $topic =~ /^[\w\:]+$/ ) {
    (my $lib = $topic . '.pm' ) =~ s|::|/|go;
    if ( ! $::INC{$lib} ) { eval { local $SIG{__DIE__}; require $lib } }
    $topic = $::INC{$lib} if ( $::INC{$lib} );
  }
  
  Term::ShellKit::require_package( $PODViewer );
  $PODViewer->new()->parse_from_file( $topic );
  return;
}

######################################################################

use vars qw( %CommandAliases );
%CommandAliases = (
  '"' => 'echo',
  '$' => 'eval',
  '!' => 'execute',
  'q' => 'exit',
  'quit' => 'exit',
  '?' => 'help',
);

sub alias {
  if ( scalar @_ ) {
    my $cmd = shift;
    if ( scalar @_ ) {
      $CommandAliases{ $cmd } = join ' ', @_;
      return;
    } else {
     $CommandAliases{ $cmd };
    }
  } else {
    map "$_: $CommandAliases{ $_ }", sort keys %CommandAliases;
  }
}

sub _shell_rewrite {
  my $input = shift;
  
  $input =~ s/\A\s+//;
  my ($command, $args) = split(' ', $input, 2);
  my $alias = $CommandAliases{ $command }
      or return;
  
  return $alias . ( length($args) ? " $args" : '' );
}

######################################################################

1;

__END__