perldl - Simple shell for PDL

- NAME
- SYNOPSIS
- DESCRIPTION

- Command-line options
- Terminating `perldl`
- Shortcuts and aliases
- The startup file ~/.perldlrc
- Shell variables
- Executing scripts from the `perldl` prompt
- Automatically execute your own hooks
- Command preprocessing
- `perldl` and PDL::NiceSlice

# NAME

perldl - Simple shell for PDL

# SYNOPSIS

            %> perldl
            perldl> $a=sequence(10) # or any other PDL command

# DESCRIPTION

The program perldl is a simple shell (written in perl) for interactive use of PDL. perl/PDL commands can simply be typed in - and edited if you have appropriate version of the ReadLines and ReadKeys modules installed. In that case perldl also supports a history mechanism where the last 50 commands are always stored in the file .perldl_hist in your home directory between sessions. The command `l [number]` shows you the last `number` commands you typed where `number` defaults to 20.

e.g.:

       % perldl
       ReadLines enabled
       perldl> $a = rfits "foo.fits"
       BITPIX =  -32  size = 88504 pixels
       Reading  354016 bytes
       BSCALE =  &&  BZERO =

       perldl> imag log($a+400)
       Displaying 299 x 296 image from 4.6939525604248 to 9.67116928100586 ...

## Command-line options

-tk
  Load Tk when starting the shell (the perl Tk module, which is available from CPAN must be installed). This enables readline event loop processing.

-f file
  Loads the file before processing any user input. Any errors during the execution of the file are fatal.

-w
  Runs with warning messages (i.e. the normal perl `-w` warnings) turned-on.

-M module
  Loads the module before processing any user input. Compare corresponding `perl` switch.

-m module
  Unloads the module before processing any user input.

-I directory
  Adds directory to the include path. (i.e. the @INC array) Compare corresponding `perl` switch.

## Terminating `perldl`

A `perldl` session can be terminated with any of the commands `quit`, `exit` or the shorthands `x` or `q`.

## Shortcuts and aliases

- The shell aliases `p` to be a convenient short form of `print`, e.g.

         perldl> p ones 5,3

         [
          [1 1 1 1 1]
          [1 1 1 1 1]
          [1 1 1 1 1]
         ]

- `q` and `x` are short-hand for `quit`.

- `l` lists the history buffer

        perldl> l # list last 20 commands

        perldl> l 40 # list last 40 commands

- `?` is an alias for help

        perldl> ? wpic

- `??` is an alias for apropos

        perldl> ?? PDL::Doc

- help, apropos, usage and sig: all words after these commands are used verbatim and not evaluated by perl. So you can write, e.g.,

          help help

  instead of

          help 'help'

## The startup file ~/.perldlrc

If the file ~/.perldlrc is found it is sourced at start-up to load default modules, set shell variables, etc. If it is NOT found the distribution file PDL/default.perldlrc is read instead. This loads various modules considered useful by default, and which ensure compatibility with v1.11. If you don't like this and want a more streamlined set of your own favourite modules simple create your own ~/.perldlrc

To set even more local defaults the file local.perldlrc (in the current directory) is sourced if found. This lets you load modules and define subroutines for the project in the current directory.

The name is chosen specfically because it was found hidden files were NOT wanted in this circumstances.

## Shell variables

Shell variables: (Note: if you don't like the defaults change them in ~/.perldlrc)

- $PERLDL::ESCAPE - default value '#'

  Any line starting with this character is treated as a shell escape. The default value is chosen because it escapes the code from the standard perl interpreter.

- $PERLDL::PAGER - default value `more`

  External program to filter the output of commands. Using `more` prints output one screenful at a time. On Unix, setting `page(1)` and $PERLDL::PAGER to `tee -a outfile` will keep a record of the output generated by subsequent perldl commands (without paging).

- $PERLDL::PROMPT - default value 'perldl> '

  Enough said But can also be set to a subroutine reference, e.g. $PERLDL::PROMPT = sub {join(':',(gmtime)[2,1,0]).'> '} puts the current time into the prompt.

- $HOME

  The user's home directory

- $PERLDL::TERM

  This is the Term::ReadLine object associated with the perldl shell. This can be used by routines called from perldl if your command is interactive.

## Executing scripts from the `perldl` prompt

A useful idiom for developing perldl scripts or editing functions on-line is

          perldl> # emacs script &
                          -- add perldl code to script and save the file
          perldl> do 'script'

-- substitute your favourite window-based editor for 'emacs' (you may also need to change the '&' on non-Unix systems).

Running ``do 'script''' again updates any variables and function definitions from the current version of 'script'.

## Automatically execute your own hooks

The variable @PERLDL::AUTO is a simple list of perl code strings and/or code reference. It is used to define code to be executed automatically every time the user enters a new line.

A simple example would be to print the time of each command:

     perldl> push @PERLDL::AUTO,'print scalar(gmtime),"\n"'

     perldl> print zeroes(3,3)
     Sun May  3 04:49:05 1998

     [
      [0 0 0]
      [0 0 0]
      [0 0 0]
     ]

     perldl> print "Boo"
     Sun May  3 04:49:18 1998
     Boo
     perldl>

Or to make sure any changes in the file 'local.perldlrc' are always picked up :-

     perldl> push @PERLDL::AUTO,"do 'local.perldlrc'"

This code can of course be put *in* 'local.perldlrc', but be careful :-) [Hint: add `unless ($started++)` to above to ensure it only gets done once!]

Another example application is as a hook for Autoloaders (e.g. PDL::AutoLoader) to add code too which allows them to automatically re-scan their files for changes. This is extremely convenient at the interactive command line. Since this hook is only in the shell it imposes no inefficiency on PDL scripts.

Finally note this is a very powerful facility - which means it should be used with caution!

## Command preprocessing

NOTE: This feature is used by default by PDL::NiceSlice. See below for more about slicing at the `perldl` prompt

In some cases, it is convenient to process commands before they are sent to perl for execution. For example, this is the case where the shell is being presented to people unfamiliar with perl but who wish to take advantage of commands added locally (eg by automatically quoting arguments to certain commands).

The variable $PERLDL::PREPROCESS can be set to a code reference (usually in a local configuration file) that will be called, with the current string as argument, just prior to the string being executed by the shell. The modified string should be returned.

The following code would check for a call to function 'mysub' and bracket arguments with qw.

     $PERLDL::PREPROCESS = sub {
       my $str = shift;
       $str =~ s/^\s+//;  # Strip leading space
       if ($str =~ /^mysub/) {
         my ($command, $arguments) = split(/\s+/,$str, 2);
         $str = "$command qw( $arguments )"
           if (defined $arguments && $arguments !~ /^qw/);
       };
       # Return the input string, modified as required
       return $str;
     };

This would convert:

      perldl> mysub arg1 arg2

to

      perldl> mysub qw( arg1 arg2 )

which perl will understand as a list. Obviously, a little more effort is required to check for cases where the caller has supplied a normal list (and so does not require automatic quoting) or variable interpolation is required.

## `perldl` and PDL::NiceSlice

PDL::NiceSlice introduces a more convenient slicing syntax for piddles. In current versions of `perldl` niceslicing is enabled by default (if the required CPAN modules are installed on your machine).

At startup `perldl` will let you know if niceslicing is enabled. The startup message will contain info to this end, something like this:

       perlDL shell v1.XX
        PDL comes with ABSOLUTELY NO WARRANTY. For details, see the file
        'COPYING' in the PDL distribution. This is free software and you
        are welcome to redistribute it under certain conditions, see
        the same file for details.
       ReadLines, NiceSlice  enabled
       Reading /home/csoelle/.perldlrc...
       Type 'demo' for online demos
       Loaded PDL v2.XX

When you get such a message that indicates `NiceSlice` is enabled you can use the enhanced slicing syntax:

      perldl> $a = sequence 10;
      perldl> p $a(3:8:2)

For details consult the PDL::NiceSlice manpage.

PDL::NiceSlice installs a filter in the `$PERLDL::PREPROCESS` variable (see above) to enable the enhanced slicing syntax. You can use a few commands in the `perldl` shell to switch this preprocessing on or off and also explicitly check the substitutions that the NiceSlice filter makes.

You can switch the PDL::NiceSlice filter on and off by typing

      perldl> trans # switch niceslicing on

and

      perldl> notrans # switch niceslicing off

respectively. The filter is on by default.

To see how your commands are translated switch reporting on:

      perldl> report 1;
      perldl> p $a(3:8:2)
     processed p $a->nslice([3,8,2])
     [3 5 7]

Similarly, switch reporting off as needed

      perldl> report 0;
      perldl>  p $a(3:8:2)
     [3 5 7]

Reporting is off by default.