1PERLDL(1) User Contributed Perl Documentation PERLDL(1)
2
3
4
6 perldl - Simple shell for PDL (see also pdl2)
7
9 Use PDL interactively:
10
11 bash$ perldl
12 pdl> $x = sequence(10) # or any other perl or PDL command
13
14 bash$ pdl
15 pdl> print "Hello, world!\n";
16
17 Run a script:
18
19 bash$ cat > pdlscript
20 #!/usr/bin/pdl
21 print "Hello, world!\n";
22 ...
23
25 The program perldl is a simple shell (written in perl) for interactive
26 use of PDL. It consists of a command-line interface that supports
27 immediate interpretation of perl commands and expressions. Perl
28 expressions, including PDL constructs, can be entered directly at the
29 keyboard and are compiled and executed immediately. The syntax is not
30 exactly identical to Perl, in that under most circumstances ending a
31 line causes immediate execution of the command entered so far (no
32 trailing ';' is required).
33
34 The synonym pdl is a compiled executable that is useful as a script
35 interpreter using UNIX shebang ("#!") syntax. This is useful for
36 generating and re-executing command-journal files from perldl.
37
38 The perldl shell runs an initial startup file ("~/.perldlrc") that can
39 be used to pre-load perl modules or configure the global perl
40 environment. It features a path mechanism for autoloading perl
41 subroutines. There is a command-history mechanism, and several other
42 useful features such as command preprocessing, shortcuts for commonly
43 used commands such as "print", and the ability to execute arbitrary
44 code whenever a prompt is printed.
45
46 Depending on your configuration settings, perldl can be set to honor or
47 ignore the ^D (end-of-file) character when sent from a terminal, or to
48 attempt to do the Right Thing when a block construct spanning multiple
49 lines is encountered.
50
51 perldl and pdl support several command-line options, which are
52 discussed near the end of this document.
53
54 Reference manual & online help
55 The PDL reference manual and online help are available from within
56 perldl, using the help and apropos commands (which may also be
57 abbreviated ? and ??.) The help command alone prints a summary of
58 help syntax, and help <module-name> will print POD documentation from
59 the module you mention (POD is the Perl format for embedding
60 documentation in your perl code; see perlpod for details).
61
62 If you include POD documentation in your autoload subroutines (see path
63 mechanism below), then both help and apropos will find it and be able
64 to format and display it on demand.
65
66 History mechanism
67 If you have the perl modules ReadLines and ReadKeys installed, then
68 perldl supports a history and line-editing mechanism using editing keys
69 similar to emacs. The last 500 commands are always stored in the file
70 .perldl_hist in your home directory between sessions. Set
71 $PERLDL::HISTFILESIZE to change the number of lines saved. The command
72 "l [number]" shows you the last "number" commands you typed where
73 "number" defaults to 20.
74
75 e.g.:
76
77 bash$ perldl
78 ReadLines enabled
79 pdl> $x = rfits "foo.fits"
80 BITPIX = -32 size = 88504 pixels
81 Reading 354016 bytes
82 BSCALE = && BZERO =
83
84 pdl> imag log($x+400)
85 Displaying 299 x 296 image from 4.6939525604248 to 9.67116928100586 ...
86
87 Command execution
88 If you enter a simple command at the perldl command line, it is
89 immediately executed in a Perl "eval()". The environment is almost
90 identical to that within a perl script, with some important exceptions:
91
92 · $_ is not preserved across lines
93
94 $_ is used to hold the command line for initial processing, so at
95 the beginning of processing of each command line, $_ contains the
96 command itself. Use variables other than $_ to store values across
97 lines.
98
99 · Scope is not preserved across lines
100
101 Each command line is executed in a separate "eval" block within
102 perl, so scoping commands such as "my" and "local" may not perform
103 exactly as expected -- in particular, if you declare a variable with
104 "my", it is local to the particular command line on which you typed
105 the "my" command, which means that it will evaporate before the next
106 prompt is printed. (You can use "my" variables in a multi-line
107 block or to isolate values within a single command line, of course).
108
109 NOTE: pdl2 preserves lexical scope between lines.
110
111 · Execution is immediate
112
113 Under most circumstances, as soon as you end a line of input the
114 line is parsed and executed. This breaks Perl's normal dependence
115 on semicolons as command delimiters. For example, the two-line
116 expression
117
118 print "Hello ",
119 "world";
120
121 prints the phrase "Hello world" in Perl, but (under most
122 circumstances) "Hello " in perldl.
123
124 · Multi-line execution
125
126 In multiline mode (which is enabled by default, see Shell variables,
127 below), perldl searches for searches for block-like constructs with
128 curly braces, parentheses, quotes, and related delimiters. If you
129 leave such a construct open, perldl accepts more lines of input
130 until you close the construct or explicitly end the multi-line
131 expression with ^D. Following the example above, the phrase
132
133 { print "Hello ",
134 "world"; }
135
136 will print "Hello world" from either Perl or (in multi-line mode)
137 perldl.
138
139 Warning: The multi-line parsing uses Damian Conway's Text::Balanced
140 module, which contains some flaws -- so it can be fooled by quote-
141 like operators such as "q/.../", included POD documentation, multi-
142 line "<<" quotes, and some particularly bizarre-but-valid "m/.../"
143 matches and "s/.../.../" substitutions. In such cases, use ^D to
144 close out the multi-line construct and force compilation-and-
145 execution.
146
147 If you want to preserve this behavior in a script (for example to
148 replay a command journal file; see below on how to create one), you can
149 use pdl instead of perl as the interpreter in the script's initial
150 shebang line.
151
152 Terminating "perldl"
153 A "perldl" session can be terminated with any of the commands "quit",
154 "exit" or the shorthands "x" or "q". If EOF handling is switched on
155 (the default) you can also type ^D at the command prompt.
156
157 If the command input is NOT a terminal (for example if you are running
158 from a command journal file), then EOF will always terminate perldl.
159
160 Terminating commands (Ctrl-C handling)
161 Commands executed within "perldl" can be terminated prematurely using
162 "Ctrl-C" (or whichever key sequence sends an INT signal to the process
163 on your terminal). Provided your PDL code does not ignore "sigint"s
164 this should throw you back at the "perldl" command prompt:
165
166 pdl> $result = start_lengthy_computation()
167 <Ctrl-C>
168 Ctrl-C detected
169
170 pdl>
171
172 Shortcuts and aliases
173 · The shell aliases "p" to be a convenient short form of "print",
174 e.g.
175
176 pdl> p ones 5,3
177
178 [
179 [1 1 1 1 1]
180 [1 1 1 1 1]
181 [1 1 1 1 1]
182 ]
183
184 · "q" and "x" are short-hand for "quit".
185
186 · "l" lists the history buffer
187
188 pdl> l # list last 20 commands
189
190 pdl> l 40 # list last 40 commands
191
192 · "?" is an alias for help
193
194 pdl> ? pdl2 # get help for new pdl2 shell
195
196 · "??" is an alias for apropos
197
198 pdl> ?? PDL::Doc
199
200 · help, apropos, usage and sig: all words after these commands are
201 used verbatim and not evaluated by perl. So you can write, e.g.,
202
203 pdl> help help
204
205 instead of
206
207 pdl> help 'help'
208
209 Command-line options
210 perldl and pdl support several command-line options to adjust the
211 behavior of the session. Most of them are equivalent to commands that
212 can be entered at the pdl> prompt. They are:
213
214 -glut
215 Load OpenGL when starting the shell (the perl OpenGL module, which
216 is available from CPAN must be installed). This enables readline
217 event loop processing. Don't use with -tk.
218
219 -tk Load Tk when starting the shell (the perl Tk module, which is
220 available from CPAN must be installed). This enables readline
221 event loop processing. Don't use with -glut.
222
223 -f file
224 Loads the file before processing any user input. Any errors during
225 the execution of the file are fatal.
226
227 -w Runs with warning messages (i.e. the normal perl "-w" warnings)
228 turned-on.
229
230 -M module
231 Loads the module before processing any user input. Compare
232 corresponding "perl" switch.
233
234 -m module
235 Unloads the module before processing any user input.
236
237 -I directory
238 Adds directory to the include path. (i.e. the @INC array) Compare
239 corresponding "perl" switch.
240
241 -V Prints a summary of PDL config. This information should be included
242 with any PDL bug report. Compare corresponding "perl" switch.
243
244 The startup file ~/.perldlrc
245 If the file ~/.perldlrc is found it is sourced at start-up to load
246 default modules, set shell variables, etc. If it is NOT found the
247 distribution file PDL/default.perldlrc is read instead. This loads
248 various modules considered useful by default, and which ensure
249 compatibility with v1.11. If you don't like this and want a more
250 streamlined set of your own favourite modules simple create your own
251 ~/.perldlrc. You may wish to start from the existing
252 PDL/default.perldlrc as a template since it will not be sourced once
253 you replace it with your own version.
254
255 To set even more local defaults the file local.perldlrc (in the
256 current directory) is sourced if found. This lets you load modules and
257 define subroutines for the project in the current directory.
258
259 The name is chosen specifically because it was found hidden files were
260 NOT wanted in these circumstances.
261
262 The startup file should normally include "use PDL::AutoLoader;", as
263 many of the nicer interactive features won't work without it.
264
265 Shell variables
266 Shell variables: (Note: if you don't like the defaults change them in
267 ~/.perldlrc)
268
269 · $PERLDL::ESCAPE - default value '#'
270
271 Any line starting with this character is treated as a shell escape.
272 The default value is chosen because it escapes the code from the
273 standard perl interpreter.
274
275 · $PERLDL::HISTFILESIZE - default value 500
276
277 This is the number of lines of perldl shell command history to
278 keep.
279
280 · $PERLDL::PAGER - default value "more"
281
282 External program to filter the output of commands. Using "more"
283 prints output one screenful at a time. On Unix, setting page(1)
284 and $PERLDL::PAGER to "tee -a outfile" will keep a record of the
285 output generated by subsequent perldl commands (without paging).
286
287 · $PERLDL::PROMPT - default value 'pdl> '
288
289 Enough said But can also be set to a subroutine reference, e.g.
290 $PERLDL::PROMPT = sub {join(':',(gmtime)[2,1,0]).'> '} puts the
291 current time into the prompt.
292
293 · $PERLDL::MULTI - default value 1
294
295 If this is set to a true value, then perldl will parse multi-line
296 perl blocks: your input will not be executed until you finish a
297 line with no outstanding group operators (such as quotes, blocks,
298 parenthesis, or brackets) still active. Continuation lines have a
299 different prompt that shows you what delimiters are still active.
300
301 Note that this is not (yet!) a complete perl parser. In
302 particular, Text::Balanced appears to be able to ignore quoting
303 operatores like "q/ ... /" within a line, but not to be able to
304 extend them across lines. Likewise, there is no support for the
305 '<<' operator.
306
307 Multiline conventional strings and {}, [], and () groupings are
308 well supported.
309
310 · $PERLDL::NO_EOF - default value 0 / 1 on MSWin32
311
312 Protects against accidental use of "^D" from the terminal. If this
313 is set to a true value, then you can't accidentally exit perldl by
314 typing "^D". If you set it to a value larger than 1 (and
315 PERLDL::MULTI is set), then you can't use "^D" to exit multiline
316 commands either. If you're piping commands in from a file or pipe,
317 this variable has no effect.
318
319 · $HOME
320
321 The user's home directory
322
323 · $PERLDL::TERM
324
325 This is the Term::ReadLine object associated with the perldl shell.
326 It can be used by routines called from perldl if your command is
327 interactive.
328
329 · $PDL::toolongtoprint
330
331 The maximal size pdls to print (defaults to 10,000 elements). This
332 is not just a "perldl" or "pdl2" variable but it is something that
333 is usually needed in an interactive debugging session.
334
335 Executing scripts from the "perldl" prompt
336 A useful idiom for developing perldl scripts or editing functions on-
337 line is
338
339 pdl> # emacs script &
340 -- add perldl code to script and save the file
341
342 pdl> do 'script'
343
344 -- substitute your favourite window-based editor for 'emacs' (you may
345 also need to change the '&' on non-Unix systems).
346
347 Running "do 'script'" again updates any variables and function
348 definitions from the current version of 'script'.
349
350 Executing perldl scripts from the command line
351 PDL scripts are just perl scripts that happen to use PDL (and possibly
352 PDL::NiceSlice). But for the truly lazy, perldl can be invokes as a
353 script interpreter. Because perldl is itself an interpreted perl
354 script, most unices won't allow you to say "#!/usr/bin/perldl" at the
355 top of your script.
356
357 Instead, say "#!/usr/bin/pdl" and your script will be executed exactly
358 as if you typed it, line-by-line, into the perldl shell.
359
360 Command preprocessing
361 NOTE: This feature is used by default by PDL::NiceSlice. See below for
362 more about slicing at the "perldl" prompt
363
364 In some cases, it is convenient to process commands before they are
365 sent to perl for execution. For example, this is the case where the
366 shell is being presented to people unfamiliar with perl but who wish to
367 take advantage of commands added locally (eg by automatically quoting
368 arguments to certain commands).
369
370 *NOTE*: The preprocessing interface has changed from earlier versions!
371 The old way using $PERLDL::PREPROCESS will still work but is strongly
372 deprecated and might go away in the future.
373
374 You can enable preprocessing by registering a filter with the
375 "preproc_add" function. "preproc_add" takes one argument which is the
376 filter to be installed. A filter is a Perl code reference (usually set
377 in a local configuration file) that will be called, with the current
378 command string as argument, just prior to the string being executed by
379 the shell. The modified string should be returned. Note that you can
380 make "perldl" completely unusable if you fail to return the modified
381 string; quitting is then your only option.
382
383 Filters can be removed from the preprocessing pipeline by calling
384 "preproc_del" with the filter to be removed as argument. To find out
385 if a filter is currently installed in the preprocessing pipeline use
386 "preproc_registered":
387
388 pdl> preproc_add $myfilter unless preproc_registered $myfilter;
389
390 Previous versions of "perldl" used the variable $PERLDL::PREPROCESS.
391 This will still work but should be avoided. Please change your scripts
392 to use the "preproc_add" etc functions.
393
394 The following code would check for a call to function 'mysub' and
395 bracket arguments with qw.
396
397 $filter = preproc_add sub {
398 my $str = shift;
399 $str =~ s/^\s+//; # Strip leading space
400 if ($str =~ /^mysub/) {
401 my ($command, $arguments) = split(/\s+/,$str, 2);
402 $str = "$command qw( $arguments )"
403 if (defined $arguments && $arguments !~ /^qw/);
404 };
405 # Return the input string, modified as required
406 return $str;
407 };
408
409 This would convert:
410
411 pdl> mysub arg1 arg2
412
413 to
414
415 pdl> mysub qw( arg1 arg2 )
416
417 which Perl will understand as a list. Obviously, a little more effort
418 is required to check for cases where the caller has supplied a normal
419 list (and so does not require automatic quoting) or variable
420 interpolation is required.
421
422 You can remove this preprocessor using the "preproc_del" function which
423 takes one argument (the filter to be removed, it must be the same
424 coderef that was returned from a previous "preproc_add" call):
425
426 pdl> preproc_del $filter;
427
428 An example of actual usage can be found in the "perldl" script. Look at
429 the function "trans" to see how the niceslicing preprocessor is
430 enabled/disabled.
431
432 "perldl" and PDL::NiceSlice
433 PDL::NiceSlice introduces a more convenient slicing syntax for piddles.
434 In current versions of "perldl" and "pdl2" niceslicing is enabled by
435 default (if the required CPAN modules are installed on your machine).
436
437 At startup "perldl" will let you know if niceslicing is enabled. The
438 startup message will contain info to this end, something like this:
439
440 perlDL shell v1.XX
441 PDL comes with ABSOLUTELY NO WARRANTY. For details, see the file
442 'COPYING' in the PDL distribution. This is free software and you
443 are welcome to redistribute it under certain conditions, see
444 the same file for details.
445 ReadLines, NiceSlice enabled
446 Reading /home/csoelle/.perldlrc...
447 Type 'demo' for online demos
448 Loaded PDL v2.XX
449
450 When you get such a message that indicates "NiceSlice" is enabled you
451 can use the enhanced slicing syntax:
452
453 pdl> $x = sequence 10;
454 pdl> p $x(3:8:2)
455
456 For details consult PDL::NiceSlice.
457
458 PDL::NiceSlice installs a filter in the preprocessing pipeline (see
459 above) to enable the enhanced slicing syntax. You can use a few
460 commands in the "perldl" shell to switch this preprocessing on or off
461 and also explicitly check the substitutions that the NiceSlice filter
462 makes.
463
464 You can switch the PDL::NiceSlice filter on and off by typing
465
466 pdl> trans # switch niceslicing on
467
468 and
469
470 pdl> notrans # switch niceslicing off
471
472 respectively. The filter is on by default.
473
474 To see how your commands are translated switch reporting on:
475
476 pdl> report 1;
477 pdl> p $x(3:8:2)
478 processed p $x->nslice([3,8,2])
479 [3 5 7]
480
481 Similarly, switch reporting off as needed
482
483 pdl> report 0;
484 pdl> p $x(3:8:2)
485 [3 5 7]
486
487 Reporting is off by default.
488
489 Automatically execute your own hooks
490 The variable @PERLDL::AUTO is a simple list of perl code strings and/or
491 code reference. It is used to define code to be executed automatically
492 every time the user enters a new line.
493
494 A simple example would be to print the time of each command:
495
496 pdl> push @PERLDL::AUTO,'print scalar(gmtime),"\n"'
497
498 pdl> print zeroes(3,3)
499 Sun May 3 04:49:05 1998
500
501 [
502 [0 0 0]
503 [0 0 0]
504 [0 0 0]
505 ]
506
507 pdl> print "Boo"
508 Sun May 3 04:49:18 1998
509 Boo
510 pdl>
511
512 Or to make sure any changes in the file 'local.perldlrc' are always
513 picked up :-
514
515 pdl> push @PERLDL::AUTO,"do 'local.perldlrc'"
516
517 This code can of course be put *in* 'local.perldlrc', but be careful
518 :-) [Hint: add "unless ($started++)" to above to ensure it only gets
519 done once!]
520
521 Another example application is as a hook for Autoloaders (e.g.
522 PDL::AutoLoader) to add code too which allows them to automatically re-
523 scan their files for changes. This is extremely convenient at the
524 interactive command line. Since this hook is only in the shell it
525 imposes no inefficiency on PDL scripts.
526
527 Finally note this is a very powerful facility - which means it should
528 be used with caution!
529
530
531
532perl v5.32.0 2020-09-17 PERLDL(1)