[comp.lang.perl] chsh /usr/bin/perl writing a shell in perl

mayer@sono.uucp (Ronald &) (06/27/91)

In a recent article tchrist@convex.COM (Tom Christiansen) writes:
>
>It would be too hard to type commands in, which after all is what shells
>are all about.  Everything would be system thing, or print `foo` that.
>

I don't think that was the question.  I'm rather sure the original
poster intended to run a perl script as his login shell.  [Anyway,
perl with now arguments or script is a pretty boring shell because the
only way I can get it to eval anything is with an EOF.]  If he
believes everything the FAQ says about interactive perl scripts, he
might be tempted to use something like "perl -de 0" as a login shell,
but this is rather clumsy.

It's quite trivial to make a reasonable shell in perl.  All you need
to do is have a script which does this:

loop forever {
    Get a line of input from the user.
    If desired, process the input by 'eval(@aliases)' or similar.
    Try to eval the input.
    If the command eval's successfully, 'next'
    If the command successfully executes as a unix program, 'next'
    If $@ tells you eval failed because of "syntax error .* at EOF",
        continue appending lines of input to the command until it
        evals successfully or it gives you a real syntax error.
        [This lets you enter multi-line perl commands like for loops.]
} continue {
    print the output of the eval or unix_command
}

The GOOD thing about using perl scripts as a shell is that you can
program it to use whatever aliasing, history, builtin-command,
flow-control, etc, etc, mechanism you want to apply to the user's
input.

    Ron

rockwell@socrates.umd.edu (Raul Rockwell) (07/01/91)

Ronald Mayer:
   It's quite trivial to make a reasonable shell in perl.  All you need
   to do is have a script which does this:
[long description elided]

Why go to all that trouble?  If you want multi-line statements, $/=';'
Or, $/="\n\n"  If you want history, run inside emacs or something... 

#!/usr/bin/perl
print $prompt="   ";
print (eval || system), print "\n".$prompt while <STDIN>;

Of course, I happen to like seeing return codes and so on...

-- 
Raul <rockwell@socrates.umd.edu>

rockwell@socrates.umd.edu (Raul Rockwell) (07/01/91)

Raul Rockwell:

   #!/usr/bin/perl
   print $prompt="   ";
   print (eval || system), print "\n".$prompt while <STDIN>;

That's a bit crude, even for my taste, try:
print (eval || $@ && system), print "\n".$prompt while <STDIN>;

This at least recognizes boundary conditions...

-- 
Raul <rockwell@socrates.umd.edu>