CSCI A348
Mastering the World-Wide Web

Spring `96 Assignment #1
Perl Shell - A Solution

#!/usr/local/bin/perl -s

$prompt = $ENV{'PSH_PROMPT'} || 'psh> ';
$shell  = $ENV{'SHELL'}      || 'csh';

# psh - a simple perl shell, interactively run shell and/or perl commands
# Steve Kinzler, kinzler@cs.indiana.edu, Jan 96
# explanatory markup by Steve Hughes, sthughes@cs.indiana.edu, Feb 96

$usage = "usage: $0 [ -s ] [ -p ] [ script [ argument ... ] ]
        -s      unspecified commands are taken to be shell commands to run
        -p      print the values of all evaluated perl expressions
Input lines are either run as shell commands (if they begin with ';')
or are evaluated as perl expressions (if they begin with ':').  If a
line begins with neither ';' nor ':', it's assumed to be perl unless
the -s option is given.  If a script file is given, it is used as the
input source.  Any script arguments are stored in \@argv.\n";

die $usage if $h;

if (@ARGV) {
        $script = shift;
        @argv   = @ARGV;
        @ARGV   = ($script);
}

$prompt = '' if @ARGV || ! -t;
$|      = 1;

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

print $prompt;
while (<>) {
        chop;

        if (s/^\s*;\s*// || ! s/^\s*:\s*// && $s) {
                system $shell, '-c', $_;
        } else {
                $value = eval;
                warn $@            if $@;
                print $value, "\n" if $p;
        }
        print $prompt;
}
<sthughes@cs.indiana.edu> and <kinzler@cs.indiana.edu> 2 February 1996