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