Making it yours#

Settings#

:set shows the settings, :set with a name shows one, and with a name and a value changes it:

ipps> :set
  autorun    off        an unknown sub of main runs a program: on or off
  max_depth  6          levels shown of a nested structure; 0: all
  max_items  24         elements shown of an array or hash; 0: all
  max_string 240        characters shown of a string; 0: all
  prompt     'ipps> '   the prompt
  prompt2    ' ...> '   the prompt for unfinished input
  shell      '/bin/bash -c' what "! command" runs the command with
  time       off        show the times of every line: on or off
  width      0          columns for a printed value; 0: the terminal's
ipps> :set max_items 5
ipps> [1 .. 30]
[1, 2, 3, 4, 5, ... (25 more)]

A prompt in quotes keeps its trailing space:

ipps> :set prompt "perl> "
perl>

A setting made this way lasts for the session. To keep it, put it in the start file.

The start file#

ipps keeps two files in ~/.pperl/ipps, or in $PPERL_HOME/ipps when that variable is set:

rc.pl

Perl that runs in the session before the first prompt. What it declares is there when you start typing.

history

The lines you typed, read at the start and written at the end.

rc.pl is ordinary Perl, so it can load the modules you always want and define the subs you always use:

use List::Util qw(sum max min first);
use JSON::PP ();

sub slurp { my ($file) = @_; open my $fh, '<', $file or die "$file: $!"; local $/; <$fh> }

Start ipps with --norc to leave it out for once.

Settings, macros and commands in the start file#

Three calls reach the prompt itself from rc.pl.

A setting:

Peta::Shell->set(max_items => 100);
Peta::Shell->set(time => 'on');

A macro is a word that is replaced when it is the first word of a line and a space or the end of the line follows it. Given a string, the word is replaced and the rest of the line stays:

Peta::Shell->macro(p => 'print');
ipps> p "printed by a macro\n"
printed by a macro

Given a sub, the macro gets the rest of the line and returns the Perl to run in its place:

Peta::Shell->macro(sq => sub { my ($rest) = @_; "($rest) ** 2" });
ipps> sq 3 + 4
49

A command of your own is called with a colon, like the built-in ones, and gets the rest of the line:

Peta::Shell->command(greet => sub { my ($shell, $rest) = @_; print "hello, $rest\n" });
ipps> :greet world
hello, world

:help lists your commands and macros after its own. The names of the built-in commands are taken.

Programs as functions#

With the autorun setting on, a call of a sub that does not exist runs the program of that name instead and returns what the program wrote:

ipps> :set autorun on
ipps> whoami()
"rj\n"
ipps> my @manifests = ls('Cargo.*')
("Cargo.lock\n", "Cargo.toml\n")

In list context the output is a list of lines, otherwise one string. The arguments are joined with spaces into a command line for /bin/sh, so the shell still expands the * above. What the program writes to its error output appears on the terminal, and its exit status is in $?:

ipps> false(); $? >> 8
1

Perl’s rules for calling a sub still decide what you can type:

  • Without parentheses a name must be declared first, and :programs declares it. After :programs ls whoami, ls 'Cargo.*' and a bare whoami work. Before it, the first is a syntax error and the second is refused under use strict.

  • Every argument is Perl. In ls -l the -l is Perl’s file test, not an option; write ls '-l'.

  • A name of one of Perl’s own functions, such as kill or sleep, stays Perl’s, and :programs refuses it. A program whose name Perl cannot use for a sub, such as apt-get, cannot be called this way.

  • It works in the package main only.

A mistyped sub name now runs a program too: pritn("x") reports Can't exec "pritn": No such file or directory, not an undefined sub.

For options, pipes and redirections the shell’s own syntax is simpler, and ! command gives you exactly that.

To have autorun in every session, put it in rc.pl:

Peta::Shell->set(autorun => 'on');

autorun is main’s AUTOLOAD, and ipps does not switch it on while you have one of your own. Writing your own is the way to make programs behave differently, for example to give lines without their newlines:

use subs qw(ls cat);

sub AUTOLOAD {
    my $program = our $AUTOLOAD =~ s/.*:://r;
    return if $program eq 'DESTROY';
    chomp(my @lines = qx{$program @_});
    return wantarray ? @lines : join "\n", @lines;
}

use subs declares names for it, as :programs does for autorun.

Without a terminal#

When standard input is not a terminal, ipps reads it line by line without a prompt and prints values and errors the same way. That makes it usable in a pipe:

printf 'my @w = split " ", "the quick brown fox";\nscalar @w\njoin "-", reverse @w\n' | pperl --shell
4
'fox-brown-quick-the'

The commands work there too.