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