#!/usr/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, steve@kinzler.com, Jan 96/Oct 03
# https://kinzler.com/me/home.html#unix

$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 ':') in an array
context.  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 ($command = <>) {
	chop $command;

	if (   $command =~ s/^\s*;\s*// ||
	    ! ($command =~ s/^\s*:\s*//) && $s) {
		system $shell, '-c', $command;
	} else {
		@value = eval $command;
		warn  $@	   if $@;
		print "@value\n"   if $p;
	}

	print $prompt;
}
