Next System Programming in Perl 16

A (possibly) usable shell

        $| = 1;
        while (1) {
          print "perl% ";
          my $command = <STDIN>;
          last if $command eq '';
          chomp $command;
          my (%command) = parse($command);
          my $pid = fork;
          if (! defined $pid) { print STDERR "Couldn't fork: $!\n" }
          elsif ($pid != 0) { wait }
          # Handle redirections
          else { 
            if (defined $command{stdin}) {
              open STDIN, "<", $command{stdin} 
                or die "Couldn't redirect stdin from $command{stdin}: $!\n";
            }
            if (defined $command{stdout}) {
              open STDOUT, $command{appendmode}, $command{stdout} 
                or die "Couldn't redirect stdout to $command{stdout}: $!\n";
            }
            exec $command{program}, @{$command{args}};
            die "Couldn't exec: $!\n";
          }
        }


Next 16