Perl6-Pugs

 view release on metacpan or  search on metacpan

examples/shell.pl  view on Meta::CPAN

use v6-alpha;

# A simple shell written in Perl6

# TODO
# BACKPSACE, history, editing ?


my $prompt = '<p6shell>$ ';
my $VERSION = '0.01';

# we should have this list from some internal command
# probably along with the signature of these functions
my @available_commands = <exit print say>;
@available_commands.push( <mkdir rmdir chdir unlink chmod chown> );
@available_commands.push( <pop push> );


# Enable reading character as they ar typed, see Perl5: perldoc -f getc  
# It would be better to use Term::ReadKey but it has to be implemented for Perl6
my $BSD_STYLE = 1;

if ($BSD_STYLE) {
    system "stty cbreak </dev/tty >/dev/tty 2>&1";
}
else {
    system "stty", '-icanon', 'eol', "\x01";
}

my $_loop_ = get_loop();
eval $_loop_;

if ($BSD_STYLE) {
    system "stty -cbreak </dev/tty >/dev/tty 2>&1";
}
else {
    system "stty", 'icanon', 'eol', '^@'; # ASCII null
}
exit;

#################################################333

sub get_loop {
return '
    loop {
        my $command = "";
        print "\n", $prompt;
        loop {
            my $char = $*IN.getc;
            if ($char eq "\n") {
                # TODO: maybe check if _loop_ shows up in the input and disallow that code ?
                if (eval "$command;" ~ $_loop_ ) {
                    exit;
                } 
                else {
                    print $!;
                    last;
                }
            }
            if ($char eq "\t") {
                # clean the TAB but keep what we had so far
                refresh_commandline($command);

                my $tail = tab_completition($command);

                if (defined $tail) {
                    $command ~= $tail;
                    refresh_commandline($command);
                }
                next;
            }
            $command ~= $char;
        }
    }
';



( run in 2.165 seconds using v1.01-cache-2.11-cpan-71847e10f99 )