Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Okay, here's a very basic shell implementation…

use v5.16; package PerlShell { use Moo; use constant { RO => 'ro', RW => 'rw', LAZY => 'lazy', RWP => 'rwp +' }; use Types::Standard -types; use Types::Path::Tiny -types; use Term::ANSIColor (); use Term::ReadLine::Tiny (); use List::Util qw(uniq); use namespace::autoclean; has cwd => ( is => RW, isa => Dir, lazy => 1, builder => sub { Path->class->cwd }, ); has term => ( is => LAZY, isa => InstanceOf['Term::ReadLine::Tiny'], builder => sub { my $self = shift; my $term = 'Term::ReadLine::Tiny'->new; # $term->autocomplete(sub { $self->handle_autocomplete(@_) + }); $term; }, ); has util_namespace => ( is => LAZY, isa => Str, # package name really builder => sub { 'PerlShell::Functions' }, ); sub prompt { my $self = shift; Term::ANSIColor::colored(['bold white'], '> '); } sub prepare_environment { my $self = shift; my ($ns) = @_; no strict 'refs'; my @our; for my $var (keys %ENV) { ${"$ns\::$var"} = $ENV{$var}; push @our, "\$$var"; } ${"$ns\::SHELL"} = $self; push @our, "\$SHELL"; ${"$ns\::TERM"} = $self->term; push @our, "\$TERM"; ${"$ns\::CWD"} = $self->cwd; push @our, "\$CWD"; sprintf('our (%s);', join q[,], uniq @our); } sub format_for_output { my $self = shift; "$_[0]"; } sub run { my $self = shift; my $term = $self->term; my $package = $self->util_namespace; while (defined(my $line = $term->readline($self->prompt))) { local $@; my $prefix = $self->prepare_environment($package); my @output = eval qq{ package $package; no strict; no warn +ings; $prefix; $line }; if ($@) { say {$term->OUT} Term::ANSIColor::colored(['bold red'] +, $@); } else { say {$term->OUT} $self->format_for_output($_) for @out +put; } } say ""; say {$term->OUT} Term::ANSIColor::colored(['bold green'], 'Bye +!'); } } package PerlShell::Util { use Ref::Util (); sub croak { my $fmt = shift; die sprintf("$fmt\n", @_); } sub parse_arguments { my %options; my @arguments; my $seendashdash = 0; if (Ref::Util::is_plain_hashref($_[0])) { %options = %{ +shift }; } while (@_) { if (not Ref::Util::is_ref($_[0]) and not $seendashdash and + $_[0] =~ /\A-([\w-]+)\z/) { my $str = $1; shift; if ($str eq '-') { $seendashdash = 1; } else { $options{$str} = 1; } next; } push @arguments, shift; } return (\%options, \@arguments); } } package PerlShell::Functions { sub args { require Data::Dumper; Data::Dumper::Dumper(PerlShell::Util::parse_arguments(@_)); } sub ls { my ($options, $args) = PerlShell::Util::parse_arguments(@_); my @dirs = map Path::Tiny::->new($_), @$args; @dirs = our $CWD unless @dirs; map { $_->children } @dirs; } sub cd { my ($options, $args) = PerlShell::Util::parse_arguments(@_); PerlShell::Util::croak('cd expects 1 argument, not %d', scalar +(@$args)) unless @$args == 1; my $new; our ($CWD, $SHELL); if ($args->[0] eq '..') { $new = $CWD->parent; } elsif ($args->[0] eq '.') { $new = $CWD; } else { $new = Path::Tiny::->new($args->[0]); if ($new->is_relative) { $new = $CWD->child($new); } } $SHELL->cwd( $CWD = $new ); } } PerlShell->new->run;

It only really implements cd and ls. So you can start the shell and type:

> cd('bin') > ls() > cd('..')

And things work as you might expect. The eval which evaluates each line has strict and warnings switched off, so you can use things like barewords. Thus the following also works fine and feels slightly more shell-like:

> cd bin > ls > cd '..'

In reply to Re: Perl as interactive Shell? by tobyink
in thread Perl as interactive Shell? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-23 13:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found