NAME

Shell::Perl - A read-eval-loop in Perl

SYNOPSYS

use Shell::Perl;
Shell::Perl->run_with_args;

DESCRIPTION

This is the implementation of a command-line interpreter for Perl. I wrote this because I was tired of using irb when needing a calculator with a real language within. Ah, that and because it was damn easy to write it.

This module is the heart of the pirl script provided with Shell-Perl distribution, along with this module.

EXAMPLE SESSION

$ pirl
Welcome to the Perl shell. Type ':help' for more information


pirl @> 1+1
2

pirl @> use YAML qw(Load Dump);
()

pirl @> $data = Load("--- { a: 1, b: [ 1, 2, 3] }\n");
{ a => 1, b => [1, 2, 3] }

pirl @> $var = 'a 1 2 3'; $var =~ /(\w+) (\d+) (\d+)/
("a", 1, 2)

pirl @> :q

COMMANDS

Most of the time, the shell reads Perl statements, evaluates them and outputs the result.

There are a few commands (started by ':') that are handled by the shell itself.

:h(elp)

Handy for remembering what the shell commands are.

:q(uit)

Leave the shell. The Perl statement exit will work too.

SYNONYMS: :exit

:set out (D|DD|Y)

Changes the dumper for the expression results used before output. The current supported are:

D

Data::Dump, the default

DD

Data::Dumper, the good and old core module

Y

YAML

:set ctx (scalar|list|void|s|l|v|$|@|_)

Changes the default context used to evaluate the entered expression. The default is 'list'.

Intuitively, 'scalar', 's' and '$' are synonyms, just like 'list', 'l', and '@' or 'void', 'v', '_'.

There is a nice way to override the default context in a given expression. Just a '#' followed by one of 'scalar|list|void|s|l|v|$|@|_' at the end of the expression.

pirl @> $var = 'a 1 2 3'; $var =~ /(\w+) (\d+) (\d+)/
("a", 1, 2)

pirl @> $var = 'a 1 2 3'; $var =~ /(\w+) (\d+) (\d+)/ #scalar
1

METHODS

Remember this is an alpha version, so the API may change and that includes the methods documented here. So consider this section as implementation notes for a while.

In later versions, some of these information may be promoted to a public status. Others may be hidden or changed and even disappear without further notice.

new
$sh = Shell::Version->new;

The constructor.

run_with_args
Shell::Perl->run_with_args;

Starts the read-eval-print loop after (possibly) reading options from @ARGV. It is a class method.

run
$sh->run;

The same as run_with_args but with no code for interpreting command-line arguments. It is an instance method, so that Shell::Perl-run_with_args> is kind of:

Shell::Perl->new->run;
eval
$answer = $sh->eval($exp);
@answer = $sh->eval($exp);

Evaluates the user input given in $exp as Perl code and returns the result. That is the 'eval' part of the read-eval-print loop.

print
$sh->print(@args);

Prints a list of args at the output stream currently used by the shell. (It is just STDOUT by now.)

out
$sh->out($answer);
$sh->out(@answers);

That corresponds to the 'print' in the read-eval-print loop. It outputs the evaluation result after passing it through the current dumper.

help
$sh->help;

Outputs the help as provided by the command ":help".

reset
$sh->reset;

Does nothing by now, but it will.

set_ctx
$sh->set_ctx($context);

Assigns to the current shell context. The argument must be one of ( 'scalar', 'list', 'void', 's', 'l', 'v', '$', '@', '_' ) .

set_out
$sh->set_out($dumper);

Changes the current dumper used for printing the evaluation results. Actually must be one of "D" (for Data::Dump), "DD" (for Data::Dumper) or "Y" (for YAML).

prompt_title
$prompt = $sh->prompt_title;

Returns the current prompt which changes with executable name and context. For example, "pirl @>", "pirl $>", and "pirl >".

TO DO

There is a lot to do, as always. Some of the top priority tasks are:

  • Accept multiline statements;.

  • Refactor the code to promote easy customization of features.

SEE ALSO

This project is hosted at Google Code:

http://code.google.com/p/iperl/

To know about interactive Perl interpreters, there are two FAQS contained in perlfaq3 which are good starting points. Those are

How can I use Perl interactively?
http://perldoc.perl.org/perlfaq3.html#How-can-I-use-Perl-interactively%3f

Is there a Perl shell?
http://perldoc.perl.org/perlfaq3.html#How-can-I-use-Perl-interactively%3f

An extra list of Perl shells can be found here:

http://www.focusresearch.com/gregor/document/psh-1.1.html#other_perl_shells

BUGS

There are some quirks with Term::Readline (at least on Windows).

There are more bugs. I am lazy to collect them all and list them now.

Please report bugs via CPAN RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Shell-Perl.

AUTHOR

Adriano R. Ferreira, <ferreira@cpan.org>

Caio Marcelo, <cmarcelo@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2007 by Adriano R. Ferreira

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.