[comp.lang.perl] using perl interactively

mayer@sono.uucp (Ronald &) (05/22/91)

Has anyone written a good interface for using perl interactively?
(kinda like a over-featured calculator with string and array
operators, and access to unix commands and system calls)

I guess what I'm looking for is something like this:

  #!/usr/local/bin/perl
  $|=1;                                          # good in interactive programs
  $nprompt = ">";                                # normal prompt
  $cprompt = ">>";			         # continuation prompt
  while(1) {
      print "$input",$input?$cprompt:$nprompt;   # print the prompt
      $input  .= <>;                             # get or append to input
      $_ = @_ = eval $input;                     # eval input
      if      (!($@)) {                          # success
	  print  "@_\n";			 # show result of eval
      } elsif ($@ =~ /syntax error in file \(eval\) at line [0-9]+, at EOF/) {
	  next;                                  # probably continued statement
      } else {                                   # probably syntax error
	  print $@;                              # show eval error message
      }
      undef($input);                             # clear input
  }


which seems to do fine for all one line statements and many (but
probably not most) multi-line commands like while loops.  Is there a
better way of guessing whether a failed eval failed because of a 'real'
syntax error or if the string being eval'd is a potentially legal but
incomplete perl statement?

    Ron Mayer
    mayer@sono.uucp   sun!sono!mayer

merlyn@iwarp.intel.com (Randal L. Schwartz) (05/22/91)

In article <MAYER.91May21115429@porky.sono.uucp> you write:
| Has anyone written a good interface for using perl interactively?

There are two.  "perlsh" is buried in the distribution, which I quote
here irreverently:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% quote
#!/usr/bin/perl

# Poor man's perl shell.

# Simply type two carriage returns every time you want to evaluate.
# Note that it must be a complete perl statement--don't type double
#  carriage return in the middle of a loop.

$/ = '';	# set paragraph mode
$SHlinesep = "\n";
while ($SHcmd = <>) {
    $/ = $SHlinesep;
    eval $SHcmd; print $@ || "\n";
    $SHlinesep = $/; $/ = '';
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end quote

The other, which I use frequently, is to invoke:

	perl -de 0

which invokes the debugger on the one-line Perl program "0".  Once you
are in the debugger, it's trivial to type in Perl expressions to be
eval'ed.  And you even get a history feature and aliases that way. :-)

echo 'c' | perl -de 'print "Just another Perl hacker,"' # ugh. :-)
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/