App::PerlShell - Perl Shell
use App::PerlShell; my $shell = App::PerlShell->new(); $shell->run;
App::PerlShell creates an interactive Perl shell. From it, Perl commands can be executed. There are some additional commands helpful in any interactive shell.
I needed an interactive Perl Shell for some Perl applications I was writing and found several options on CPAN:
Perl::Shell
perlconsole
Devel::REPL
All of which are excellent modules, but none had everything I wanted without a ton of external dependencies. I didn't want that trade off; I wanted functions without dependencies.
I also wanted to emulate a shell - not necessarily a REPL (Read-Evaluate-Parse-Loop). In the way sh, bash, csh and other *nix or cmd.exe or PowerShell on Windows are a shell - I wanted a Perl Shell. For example, many of the above modules will evaluate an expression:
sh
bash
csh
cmd.exe
PowerShell
5+1 6
If I enter 5+1 in cmd.exe or bash, I don't get 6, I get an error. In a Perl program, if I have a line 5+1;, I get an error in execution. If I really want 6, I need to print 5+1;. And so it should be in the Perl Shell.
5+1
6
5+1;
print 5+1;
This is much closer to a command prompt / terminal than a REPL. As such, some basic shell commands are provided, like ls, cd and pwd for example.
ls
cd
pwd
For command recall using the up/down arrows in *nix, you will need Term::ReadLine::Gnu installed. This module will function fine without it as Term::ReadLine is a core module; however, command recall using the up/down arrows will not work.
my $shell = App::PerlShell->new([OPTIONS]);
Create a new App::PerlShell object with OPTIONS as optional parameters. Valid options are:
Option Description Default ------ ----------- ------- -execute Valid Perl code ending statements with (none) semicolon (;). -feature Perl feature set to use e.g., ":5.10" :default -homedir Specify home directory. $ENV{HOME} or Used for `cd' with no argument. $ENV{USERPROFILE} -lexical Require "my" for variables. (off) Requires Lexical::Persistence -package Package to impersonate. Execute all App::PerlShell commands as if in this package. -prompt Shell prompt. Perl> -session Session file to log commands. (none) -skipvars Variables to ignore in `variables' $VERSION, @ISA, command. @EXPORT
$shell->run();
Run the shell. Provides interactive environment for entering commands.
In the interactive shell, all valid Perl commands can be entered. This includes constructs like for () {} and if () {} ... else {} as well as any subs from use'd modules. The following are also provided.
for () {}
if () {} ... else {}
use
Change directory to optional 'directory'. No argument changes to 'homedir'. Optional return value is current directory (directory before change).
Clear screen.
Displays available commands. Commands are essentially 'sub's defined in the current package. With 'SEARCH', displays matching commands. Optional return value is array with commands.
Print command so far (don't execute) at multiline input 'More?' prompt. Must be used as debug only, no semicolon starting at first position in input.
debug
Directory listing. 'OPTIONS' are system directory listing command options. Optional return value is array of output.
Displays $var with Data::Dumper.
$var
Displays environment variables. With 'SEARCH', displays matching environment variables. Optional return value is hash with environment variables names as keys and values as values.
Exit shell.
Display shell help.
Displays used modules. With 'SEARCH', displays matching used modules. Optional return value is hash with module names as keys and file locations as values.
Open perldoc for current PACKAGE. 'OPTIONS' can start with '::' to append to current PACKAGE, be a full Package name or use perldoc command line options (e.g., -f pack).
perldoc
-f pack
Print working directory. Optional return value is result.
Open session file. Only logs Perl commands. Appends to already existing file. Use session (':close') to end.
session (':close')
List user defined variables currently active in current package in shell.
Print version information.
This module exports the COMMANDS into each Package namespace.
This distribution comes with a script (installed to the default "bin" install directory) that not only demonstrates example uses but also provides functional execution.
App::PerlShell::Config, App::PerlShell::ModRefresh, App::PerlShell::LexPersist
This software is released under the same terms as Perl itself. If you don't know what that means visit http://perl.com/.
Copyright (c) 2015-2022 Michael Vincent
http://www.VinsWorld.com
All rights reserved
To install App::PerlShell, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::PerlShell
CPAN shell
perl -MCPAN -e shell install App::PerlShell
For more information on module installation, please visit the detailed CPAN module installation guide.