forked from PDLPorters/pdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perldl.PL
1386 lines (1060 loc) · 40.5 KB
/
perldl.PL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use Config;
use File::Basename qw(&basename &dirname);
# List explicitly here the variables you want Configure to
# generate. Metaconfig only looks for shell variables, so you
# have to mention them as if they were shell variables, not
# %Config entries. Thus you write
# $startperl
# to ensure Configure will look for $Config{startperl}.
# This forces PL files to create target in same directory as PL file.
# This is so that make depend always knows where to find PL derivatives.
chdir(dirname($0));
($file = basename($0)) =~ s/\.PL$//;
$file =~ s/\.pl$//
if ($^O eq 'VMS' or $^O eq 'os2'); # "case-forgiving"
unlink $file if -f $file;
open OUT,">$file" or die "Can't create $file: $!";
print "Extracting $file (with variable substitutions)\n";
# In this section, perl variables will be expanded during extraction.
# You can use $Config{...} to use Configure variables.
print OUT <<"!GROK!THIS!";
$Config{'startperl'}
eval 'exec perl -S \$0 "\$@"'
if 0;
!GROK!THIS!
# In the following, perl variables are not expanded during extraction.
print OUT <<'!NO!SUBS!';
##########################################################################
# Here starts the actual script
# Simple shell for PDL
use vars qw($VERSION $HOME $Modules);
$VERSION = '1.357';
print "perlDL shell v$VERSION
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.\n";
# Useful shell variables
$PERLDL::ESCAPE = '#'; # Default shell escape
$PERLDL::HISTFILESIZE = 500; # Number of lines to keep in history
$PERLDL::MULTI = 1; # Enable multi-lines by default
$PERLDL::NO_EOF = 0; # Disable EOF protection by default
$PERLDL::NO_EOF = 1 if $^O =~ 'MSWin'; # ...but enable for windows
$PERLDL::PROMPT = "pdl> ";
$PERLDL::PREFIX_RE = qr(^\s*(?:pdl|perldl)>\s*); # RE for shell prompts
$PERLDL::PAGER = (exists $ENV{PAGER} ? $ENV{PAGER} : 'more');
# Default output paging program
$PERLDL::PAGE = 0;
$PERLDL::PAGING = 0;
@PERLDL::AUTO = ();
$PERLDL::PREPROCESS = undef; # old interface -- disabled
@PERLDL::PREPROCESS = (); # new preprocessor pipeline
$HOME = $ENV{HOME}; # Useful in shell
if ($^O =~ /win32/i and $HOME eq ""){
$HOME = $ENV{USERPROFILE};
$HOME =~ s/\\/\//g;
}
$,=" "; # Default
$Modules = $Modules = ""; # pacify -w
sub mypdlconfig {
require Config; # pick up perl version info
eval 'require PDL::Version' if not defined $PDL::Version::VERSION;
eval 'require PDL::Config';
eval "use Data::Dumper";
my $hasdumper = $@ eq "" ? 1 : 0;
eval "use PDL::Bad;";
my $bflag = defined($PDL::Bad::Status) && $PDL::Bad::Status;
my $txt = "\nSummary of my PDL configuration\n\n";
$txt .= "VERSION: PDL v$PDL::Version::VERSION";
$txt .= " (supports bad values)" if $bflag;
$txt .= "\n\n";
if ($hasdumper && %PDL::Config) {
$txt .= Data::Dumper->Dump([{%PDL::Config}],['%PDL::Config']);
} else {
$txt .= "Could not obtain \%PDL::Config\n";
}
$txt .= Config::myconfig(); # append perl config info
}
sub preproc_registered ($) {
my ($sub) = @_;
die "preprocessors must be code references"
unless ref $sub eq 'CODE';
return grep ($_ == $sub, @PERLDL::PREPROCESS) > 0;
}
sub preproc_add ($) {
my ($sub) = @_;
die "preprocessors must be code references"
unless ref $sub eq 'CODE';
push @PERLDL::PREPROCESS, $sub;
return $sub;
}
sub preproc_del ($) {
my ($sub) = @_;
die "preprocessors must be code references"
unless ref $sub eq 'CODE';
die "preprocessor can't be deleted: not installed"
unless preproc_registered $sub;
@PERLDL::PREPROCESS = grep ($_ != $sub, @PERLDL::PREPROCESS);
return $sub;
}
# Parse ARGV
my $read_from_file;
while(defined($_ = shift @ARGV)) {
if($_ eq "-tk") {
if ($^O eq 'MSWin32') {
print "-tk option not supported for windows\n";
next;
}
print "Using Tk";
eval "use Tk;";
if ($@ eq "") {
print " v$Tk::VERSION\n"
if defined $Tk::VERSION; # make -w happy
} else {
print ", sorry can't load module Tk\n";
}
next;
} elsif($_ eq "-glut") {
if ($^O eq 'MSWin32') {
print "-glut option not supported for windows\n";
next;
}
print "Using OpenGL for GLUT support";
eval "use OpenGL;";
if ($@ eq "") {
print " v$OpenGL::VERSION\n"
if defined $OpenGL::VERSION; # make -w happy
} else {
print ", sorry can't load module OpenGL\n";
}
OpenGL::glutInit() unless OpenGL::done_glutInit();
next;
} elsif(/^-f(.*)/) {
my $file = $1;
if(0 == length $1) {
$file = shift @ARGV;
}
print "Doing '$file'\n";
do $file;
if($@) {
die "Initialization error: $@";
}
next;
} elsif(/^-w$/){
$^W = 1;
next;
} elsif (/^-(M|m)([\w:]+)(\=\w+)?$/x) {
my ($way,$m,@im) = ($1,$2,$3?substr($3,1):());
eval "require $m";
warn, next if $@;
if ($way eq 'M') {
$m->import(@im);
} else {
$m->unimport(@im);
}
} elsif (/^-I (\S*) $/x) {
my $dir = $1;
$dir = $ARGV[++$arg]
if !$dir;
if ($dir =~ m{^ \/ }x) {
unshift(@INC, $dir);
} else {
require FindBin;
die "Error: can't find myself" if ! $FindBin::Bin;
unshift(@INC, "$FindBin::Bin/$dir");
}
} elsif (/^-V\s*$/) {
print mypdlconfig();
exit 0;
} elsif( /^-\s*$/) {
$read_from_file = 1;
last;
} else {
print << 'EOP';
Usage: perldl [options]
-glut try to load OpenGL module (Enables
readline event-loop processing).
-tk try to load Tk module (Enables
readline event-loop processing).
-f <file> execute file <file> before starting perldl
-w run with warning messages turned-on
-m <module> unload module <module>
-M <module> load module <module>
-I <dir> Add <dir> to include path.
-V print PDL version info (e.g. for a bug report)
- Following arguments are files for input.
EOP
die("Unknown argument $_");
}
}
my $readlines;
if(!$read_from_file and -t STDIN) {
eval "use Term::ReadLine";
$readlines = ($@ eq "");
} else {
$readlines=0;
}
my @enabled = ();
push @enabled, "ReadLines" if $readlines;
eval 'use PDL::NiceSlice';
unless ($@) {
my $report = 0;
sub report {
my $ret = $report;
$report = $_[0] if $#_ > -1;
return $ret;
}
my $preproc = sub { my ($txt) = @_;
my $new = PDL::NiceSlice::perldlpp('main',$txt);
print STDERR "processed $new\n" if report && $new ne $txt;
return $new;
};
sub trans {
preproc_add $preproc unless preproc_registered $preproc;
preproc_del $preproc if $#_ > -1 && !$_[0] &&
preproc_registered $preproc;
}
sub notrans { trans 0 }
trans; # switch on by default
push @enabled, "NiceSlice";
}
eval "use Text::Balanced";
my $multi_ok = ($@ eq "");
$PERLDL::MULTI = 0 unless($multi_ok);
push @enabled,"MultiLines" if $multi_ok;
print join(', ',@enabled)," enabled\n" if @enabled > 0;
if ( $readlines ){
$PERLDL::TERM = Term::ReadLine->new('perlDL', \*STDIN, \*STDOUT);
if (defined &OpenGL::done_glutInit ) {
# Attempt to use with FreeGLUT
if ($PERLDL::TERM->can('event_loop')) {
print "Using FreeGLUT event loop\n";
# Presumably, if you're using this loop, you're also selecting on other
# fileno's. It is up to you to add that in to the wait callback (first
# one passed to event_loop) and deal with those file handles.
$PERLDL::TERM->event_loop(
sub {
# This callback is called every time T::RL wants to
# read something from its input. The parameter is
# the return from the other callback.
my $fileno = shift;
my $rvec = '';
vec($rvec, $fileno, 1) = 1;
while(1) {
select my $rout = $rvec, undef, undef, 0;
last if vec($rout, $fileno, 1);
OpenGL::glutMainLoopEvent();
}
},
sub {
# This callback is called as the T::RL is starting up
# readline the first time. The parameter is the file
# handle that we need to monitor. The return value
# is used as input to the previous callback.
# We return the fileno that we will use later.
# cygwin/TRL::Gnu seems to use some other object here
# that doesn't respond to a fileno method call (rt#81344)
fileno($_[0]);
}
) unless $Term::ReadLine::toloop;
} else {
warn("Sorry, cannot use FreeGLUT with this version of ReadLine\n");
}
}
if(defined &Tk::DoOneEvent and not ref $Term::ReadLine::toloop) {
# Attempt to use with Tk
if(${$PERLDL::TERM->Features}{tkRunning}) {
print "Using Tk event loop\n";
$PERLDL::TERM->tkRunning(1);
} else {
warn("Sorry, cannot use Tk with this version of ReadLine\n");
}
}
if ( ( -e "$HOME/.perldl_hist" )
&& ( open HIST, "<$HOME/.perldl_hist" ) ) {
my @allhist = <HIST>;
close HIST;
map s/\n//g , @allhist ;
foreach (@allhist) {
$PERLDL::TERM->addhistory($_);
}
}
eval <<'EOEND';
sub END {
# Save History in $ENV{'HOME'}/.perldl_hist
# GetHistory doesn't work on all versions...
my @a= $PERLDL::TERM->GetHistory() if $PERLDL::TERM->can('GetHistory');
$#a-- if $a[-1] =~ /^(q$|x$|\s*exit\b|\s*quit\b)/; # chop off the exit command
@a= @a[($#a-$PERLDL::HISTFILESIZE+1)..($#a)] if $#a > $PERLDL::HISTFILESIZE-1 ;
if( open HIST, ">$HOME/.perldl_hist" ) {
print HIST join("\n",@a);
close HIST;
} else {
print " Unable to open \"$HOME/.perldl_hist\"\n";
}
}
EOEND
}
sub l {
if ($readlines) {
my $n = $#_ > -1 ? shift : 20;
my @h = $PERLDL::TERM->GetHistory();
my $min = $#h < $n-1 ? 0 : $#h-$n+1;
map {print "$_: $h[$_]\n"} ($min..$#h);
}
}
sub page {
$PERLDL::PAGE = (defined $_[0] ? $_[0] : 1);
}
sub nopage {
page(0);
}
sub startpage {
if ($PERLDL::PAGE) {
open(SAVEOUT, '>&STDOUT');
open(STDOUT, "| $PERLDL::PAGER");
$PERLDL::PAGING = 1;
}
}
sub endpage {
if ($PERLDL::PAGING) {
close(STDOUT);
open(STDOUT, '>&SAVEOUT');
$PERLDL::PAGING = 0;
}
}
sub startup_def {
return "PDL/default.pdl" if $^O =~ /win32/i;
return "PDL/default.perldlrc";
}
# Global and local startup
my $startup_file = -e "$HOME/.perldlrc" ? "$HOME/.perldlrc" : startup_def();
print "Reading $startup_file...\n";
eval 'require "'.$startup_file.'"';
my $PDL_OK = ($@ eq "");
if ($PDL_OK) {
require PDL::Version if not defined $PDL::Version::VERSION;
print "Type 'demo' for online demos\n";
eval "use PDL::Bad;";
my $bflag = defined($PDL::Bad::Status) && $PDL::Bad::Status;
if ( $bflag ) {
print "Loaded PDL v$PDL::Version::VERSION (supports bad values)\n";
} else {
print "Loaded PDL v$PDL::Version::VERSION\n";
}
}else{
warn "WARNING: Error loading PDL: '$@' - trying blib. \n";
eval "use blib";
delete $INC{$startup_file}; # so require will try again!
eval 'require "'.$startup_file.'"';
$PDL_OK = ($@ eq "");
if ($PDL_OK) {
require PDL::Version if not defined $PDL::Version::VERSION;
print "Loaded PDL v$PDL::Version::VERSION\n";
}else{
warn "WARNING: PDL startup not found only plain perl available\n";
$PERLDL::PROMPT = 'perl> '; # so there is visual indication of no PDL
eval << 'EOD'; # Fallback eval routine - proper one defined in PDL::Core
sub eval_and_report {
my $__code = shift; # Can be code ref or string
my $__string;
$__string = (ref $__code eq "CODE") ? '&$__code()' : $__code;
eval $__string; # Use boring eval() which misses some errors
return $@;
}
EOD
}
}
print "\nNote: AutoLoader not enabled ('use PDL::AutoLoader' recommended)\n\n"
unless defined($PDL::AutoLoader::Rescan);
if (-e 'local.perldlrc') {
print "Reading local.perldlrc ...\n";
require 'local.perldlrc' ;
}
# Short hand for some stuff
sub p { local $^W=0; print(@_); } # suppress possible undefined var message
# (dirty)
my %demos =
(
'pdl' => 'PDL::Demos::General', # have to protect pdl as it means something
'3d' => 'PDL::Demos::TriD1',
'3d2' => 'PDL::Demos::TriD2',
'3dgal' => 'PDL::Demos::TriDGallery',
'pgplot' => 'PDL::Demos::PGPLOT_demo',
'ooplot' => 'PDL::Demos::PGPLOT_OO_demo', # note: lowercase
'bad' => 'PDL::Demos::BAD_demo',
'bad2' => 'PDL::Demos::BAD2_demo',
'transform' => 'PDL::Demos::Transform_demo',
'cartography' => 'PDL::Demos::Cartography_demo',
'gnuplot' => 'PDL::Demos::Gnuplot_demo',
'prima' => 'PDL::Demos::Prima',
);
sub demo {
local $_ = lc $_[0] ;
if(/^$/) {
print <<EOD;
Use:
demo pdl # general demo
demo 3d # 3d demo (requires TriD with OpenGL or Mesa)
demo 3d2 # 3d demo, part 2. (Somewhat memory-intensive)
demo 3dgal # the 3D gallery: make cool images with 3-line scripts
demo pgplot # PGPLOT graphics output (Req.: PGPLOT)
demo OOplot # PGPLOT OO interface (Req.: PGPLOT)
demo gnuplot # Gnuplot graphics (requires PDL::Graphics::Gnuplot)
demo prima # Prima graphics (requires PDL::Graphics::Prima)
demo transform # Coordinate transformations (Req.: PGPLOT)
demo cartography # Cartographic projections (Req.: PGPLOT)
demo bad # Bad-value demo (Req.: bad value support)
demo bad2 # Bad-values, part 2 (Req.: bad value support and PGPLOT)
EOD
return;
} # if: /^$/
if ( exists $demos{$_} ) {
require PDL::Demos::Screen; # Get the routines for screen demos.
my $name = $demos{$_};
eval "require $name;"; # see docs on require for need for eval
$name .= "::run";
&{$name}();
} else {
print "No such demo!\n";
}
} # sub: demo
$SIG{'INT'} = sub { die "Ctrl-C detected\n" }; # Ctrl-C handler
my $preproc_warned = 0;
sub preproc_oldwarn {
warn << 'EOW';
Deprecated usage: $PERLDL::PREPROCESS was set.
Usage of this variable is now strongly deprecated.
To enable preprocessing with recent versions of perldl
you should use the 'preproc_add' function. For details
check the perldl manpage.
EOW
$preproc_warned = 1; # warn only once
}
#
# count_tags: Return a string containing (in order) the open brackets
# and strings in the string that is passed in. Used for multi-line parsing.
#
# Works by analysing the error message returned by Text::Balanced -- this
# is sort of fragile against changes in Text::Balanced, but what the heck.
# --CED 18-Mar-2003
#
sub count_tags {
my $s = shift;
$s =~ s/\\.//g; # Ignore all escaped characters
return undef unless($s =~ m/[^\s]/);
# [Ignore quotelike operators: they cause more trouble than they're worth!]
our($prefix,$delim,%closers);
unless(defined $prefix) {
$delim = '{[(`\'")]}';
%closers = ('{'=>'}','['=>']','('=>')');
}
# Run Text::Balanced on the string with a '{' in front of it, to
# make sure that all quoted strings are "embedded" in the outermost "{".
# The whitespace works around a short-string bug in extract_bracketed.
my $a;
my @result;
$s =~ s/^\s*\#.*$//mg; # Eliminate comment lines before extract.
eval { @result =
Text::Balanced::extract_bracketed("{".$s, $delim, $prefix);
$a = $@;
};
print "a = $a\nreturn = '",join("','",@result),"'\n"
if($PERLDL::debug);
if($a =~ m/^Did not find/) {
# No quotes -- this should never happen and is a syntax error.
print STDERR "[Error in parsing: this should never happen!]\n"
if($PERLDL::debug);
return undef;
}
elsif($a =~ m/^Unmatched emb\w+ quote \((.)\), de\w+ at offset (\d+)/) {
# Embedded quote: try to close it and reparse.
$a = $1;
return count_tags($s.$1) . $a;
}
elsif($a =~ m/^Mismatched closing bracket/) {
# This is an error condition - return false and let perl parse it
return undef;
}
elsif($a =~ m/^Unmatched opening bracket\(s\)\: \{\.\.(.\.\.)+/) {
$a = $1;
$a=~ s/\.\.//g;
return count_tags($s.$closers{$a}) . $a;
}
elsif($a =~ m/^Unmatched opening bracket\(s\)\: \{\.\.\,/) {
# Should have exactly one unmatched opening bracket.
return undef;
}
elsif(!$a) {
return undef;
}
print STDERR "Unknown error message '$a' from parser...\n"
if($PERLDL::debug);
return undef;
}
#
# process_input -- this is the central grab-some-input-and-execute it loop.
#
sub process_input {
my $lines;
if($PERLDL::MULTI && !$multi_ok) {
$PERLDL::MULTI = 0;
print STDERR "WARNING: Text::Balanced not present; disabling multi-line parsing.\n";
}
# The {} around the do let us get out with 'last' in the EOF case.
multiline: {
my $cont;
$lines = "";
do {
local $, = "";
my $prompt = $cont ? "..$cont".(" "x(5-length($cont)))."> " :
((ref $PERLDL::PROMPT) ? &$PERLDL::PROMPT : $PERLDL::PROMPT);
if ($readlines) {
$_ = $PERLDL::TERM->readline($prompt);
}else{
print $prompt if(-t ARGV); # Don't print prompt in pipes
$_ = <>;
}
if(!defined $_) {
if($cont) {
if( $PERLDL::NO_EOF > 1 && -t STDIN ) {
print STDERR "\nEOF ignored. (Close delimiters to end block. \$PERLDL::NO_EOF = $PERLDL::NO_EOF)\n";
} else {
last multiline;
}
} else {
if($PERLDL::NO_EOF && -t STDIN ) {
print STDERR "EOF ignored. ('q' or 'exit' to quit. \$PERLDL::NO_EOF = $PERLDL::NO_EOF)\n";
} else {
print STDERR "EOF detected, exiting shell.\n";
exit 0;
}
}
}
$lines .= "\n" if($cont); # Make multi-line strings work right.
$lines .= $_;
print "lines = $lines\n" if($PERLDL::debug);
} while( $PERLDL::MULTI && ($cont = count_tags($lines)) );
}
# Execute the list of auto-code
for my $c (@PERLDL::AUTO) {
my $mess = eval_and_report($c);
warn $mess if $mess;
}
# Filter out PDL shell prefixes from cut-n-pasted lines
if ( $lines =~ s/$PERLDL::PREFIX_RE// and $readlines ) {
my @hist = $PERLDL::TERM->GetHistory();
foreach my $entry (@hist) { $entry =~ s/$PERLDL::PREFIX_RE//; }
$PERLDL::TERM->SetHistory(@hist);
}
if(!defined $lines || lc $lines eq 'q' || lc $lines eq 'x' || lc $lines eq 'quit') {exit};
next if $lines =~/^\s*$/; # Blank line - do nothing
$lines =~ s/^\s*\?\?\s*/apropos /; # Make '??' = 'apropos'
$lines =~ s/^\s*\?\s*/help /; # Make lone '?' = 'help'
if ( $lines =~ /^\s*(help|usage|apropos|sig|badinfo|demo)\s+/) { # Allow help foo (no quotes)
my @t = split(/\s+/,$lines);
my $a;
foreach $a(@t) { $a=~s/^["']+//; $a=~s/['"]+$//; };
$t[1] = "'".$t[1]."'" if ($#t == 1 && !($t[1] =~ /^\$/));
$lines = join(' ',@t);
}
if (substr($lines,0,1) eq substr($PERLDL::ESCAPE,0,1) and
substr($lines,0,2) ne '#!') { # Allow escapes, avoid shebang
my @lines = split /\n/, $lines;
system(substr(shift @lines,1)); # Shell escape
$lines = join("\n",@lines);
next;
} else {
# Send code to pre-processor filters if defined
for my $filter (@PERLDL::PREPROCESS) {
$lines = $filter->($lines);
}
# honor the deprecated interface for now
if (defined $PERLDL::PREPROCESS &&
ref($PERLDL::PREPROCESS) eq 'CODE') {
preproc_oldwarn() unless $preproc_warned;
$lines = &$PERLDL::PREPROCESS($_);
}
startpage;
my $mess = eval_and_report($lines);
warn $mess if $mess;
endpage;
}
print "\n";
}
######################################################################
######################################################################
#####
##### Main loop is here! (Commands not inside any sub!)
# check for old usage of PERLDL::PREPROCESS
if (defined $PERLDL::PREPROCESS) {
preproc_oldwarn() unless $preproc_warned;
}
$|=1;
while(1) {
eval {process_input()};
if ($@) {
if ($@ =~ /Ctrl-C detected/) {
print "Ctrl-C detected\n";
next;
} else {
print "Unknown error: $@\n exiting...\n";
last;
}
}
}
#####
#####
######################################################################
######################################################################
# Work routine to eval code and post-process messages
# Currently used by 'perldl' shell
sub eval_and_report {
my $__code = shift; # Can be code ref or string
$@ = ""; # clear $@ since we might not execute the eval below
## Compile the code ref to execute. The code gets put inside {} braces
## so that there is a trivial loop (the simple block) for 'last' and 'next'
## to escape from. (Otherwise perl 5.6.1 and 5.8 do a little fandango
## on stack if you type "last" at the shell). --CED 18-Mar-2003
my $__coderef = (ref $__code eq "CODE") ? $__code : eval << "EOD"
sub {
{
$__code;
}
}
EOD
;
%@ = (); # Workaround to prevent spurious loss of $@ in early (pre-5.14 anyway) versions of perl
if( (!$@) and (ref $__coderef eq 'CODE')) {
eval { &$__coderef(); die $@ if($@); };
}
if ($@) {
my $mess = $@;
# Remove surplus parts
$mess =~ s/^\s*\(in cleanup\)\s+//; # 'cleanup ...' from Usage:...
$mess =~ s/\n\s*\(in cleanup\).*$//; # 'cleanup...'s at end
$mess =~ s/\s+at \(eval \d+\) line \d+\.?$//; # at eval ?? line ??.
return $mess; # Report error
}
return "";
}
__END__
=head1 NAME
perldl - Simple shell for PDL (see also L<pdl2>)
=head1 SYNOPSIS
Use PDL interactively:
bash$ perldl
pdl> $a = sequence(10) # or any other perl or PDL command
bash$ pdl
pdl> print "Hello, world!\n";
Run a script:
bash$ cat > pdlscript
#!/usr/bin/pdl
print "Hello, world!\n";
...
=head1 DESCRIPTION
The program B<perldl> is a simple shell (written in perl) for
interactive use of PDL. It consists of a command-line interface that
supports immediate interpretation of perl commands and expressions.
Perl expressions, including PDL constructs, can be entered directly at
the keyboard and are compiled and executed immediately. The syntax is
not exactly identical to Perl, in that under most circumstances ending
a line causes immediate execution of the command entered so far (no
trailing ';' is required).
The synonym B<pdl> is a compiled executable that is useful as a script
interpreter using UNIX shebang (C<#!>) syntax. This is useful for generating
and re-executing command-journal files from B<perldl>.
The B<perldl> shell runs an initial startup file (C<~/.perldlrc>) that can
be used to pre-load perl modules or configure the global perl environment. It
features a path mechanism for autoloading perl subroutines. There is a
command-history mechanism, and several other useful features such as command
preprocessing, shortcuts for commonly used commands such as "print",
and the ability to execute arbitrary code whenever a prompt is printed.
Depending on your configuration settings, B<perldl> can be set to
honor or ignore the ^D (end-of-file) character when sent from a
terminal, or to attempt to do the Right Thing when a block construct
spanning multiple lines is encountered.
B<perldl> and B<pdl> support several command-line options, which are
discussed near the end of this document.
=head2 Reference manual & online help
The PDL reference manual and online help are available from within
B<perldl>, using the B<help> and B<apropos> commands (which may also
be abbreviated B<?> and B<??>.) The B<help> command alone prints a summary of
help syntax, and B<< help <module-name> >> will print POD documentation
from the module you mention (POD is the Perl format for embedding
documentation in your perl code; see L<perlpod> for details).
If you include POD documentation in your autoload subroutines (see
B<path mechanism> below), then both B<help> and B<apropos> will find it
and be able to format and display it on demand.
=head2 History mechanism
If you have the perl modules ReadLines and ReadKeys installed, then
B<perldl> supports a history and line-editing mechanism using editing
keys similar to L<emacs>. The last 500 commands are always stored in
the file F<.perldl_hist> in your home directory between sessions.
Set C<$PERLDL::HISTFILESIZE> to change the number of lines saved.
The command C<l [number]> shows you the last C<number> commands you
typed where C<number> defaults to 20.
e.g.:
bash$ perldl
ReadLines enabled
pdl> $a = rfits "foo.fits"
BITPIX = -32 size = 88504 pixels
Reading 354016 bytes
BSCALE = && BZERO =
pdl> imag log($a+400)
Displaying 299 x 296 image from 4.6939525604248 to 9.67116928100586 ...
=head2 Command execution
If you enter a simple command at the B<perldl> command line, it is
immediately executed in a Perl C<eval()>. The environment is almost
identical to that within a perl script, with some important exceptions:
=over 3
=item * $_ is not preserved across lines
$_ is used to hold the command line for initial processing, so at the
beginning of processing of each command line, $_ contains the command itself.
Use variables other than $_ to store values across lines.
=item * Scope is not preserved across lines
Each command line is executed in a separate C<eval> block within perl,
so scoping commands such as C<my> and C<local> may not perform exactly
as expected -- in particular, if you declare a variable with C<my>, it
is local to the particular command line on which you typed the C<my>
command, which means that it will evaporate before the next prompt is printed.
(You can use C<my> variables in a multi-line block or to isolate values within
a single command line, of course).
NOTE: pdl2 preserves lexical scope between lines.
=item * Execution is immediate
Under most circumstances, as soon as you end a line of input the line
is parsed and executed. This breaks Perl's normal dependence on
semicolons as command delimiters. For example, the two-line expression
print "Hello ",
"world";
prints the phrase C<Hello world> in Perl, but (under most circumstances)
C<Hello > in B<perldl>.
=item * Multi-line execution
In multiline mode (which is enabled by default, see B<Shell
variables>, below), B<perldl> searches for searches for block-like
constructs with curly braces, parentheses, quotes, and related
delimiters. If you leave such a construct open, B<perldl> accepts more
lines of input until you close the construct or explictly end the multi-line
expression with ^D. Following the example above, the phrase
{ print "Hello ",
"world"; }
will print "Hello world" from either Perl or (in multi-line mode)
B<perldl>.
B<Warning>: The multi-line parsing uses Damian Conway's
L<Text::Balanced> module, which contains some flaws -- so it can be
fooled by quote-like operators such as C<q/.../>, included POD
documentation, multi-line C<E<lt>E<lt>> quotes, and some
particularly bizarre-but-valid C<m/.../> matches and C<s/.../.../>
substitutions. In such cases, use ^D to close out the multi-line construct and
force compilation-and-execution.
=back
If you want to preserve this behavior in a script (for example to replay a command
journal file; see below on how to create one), you can use B<pdl> instead of B<perl>
as the interpreter in the script's initial shebang line.
=head2 Terminating C<perldl>
A C<perldl> session can be terminated with any of the commands
C<quit>, C<exit> or the shorthands C<x> or C<q>. If EOF handling is
switched on (the default) you can also type ^D at the command prompt.
If the command input is NOT a terminal (for example if you are running
from a command journal file), then EOF will always terminate B<perldl>.
=head2 Terminating commands (Ctrl-C handling)
Commands executed within C<perldl> can be terminated prematurely
using C<Ctrl-C> (or whichever key sequence sends an INT signal
to the process on your terminal). Provided your PDL code does not
ignore C<sigint>s this should throw you back at the C<perldl>
command prompt:
pdl> $result = start_lengthy_computation()
<Ctrl-C>
Ctrl-C detected
pdl>
=head2 Shortcuts and aliases
=over
=item *
The shell aliases C<p> to be a convenient short form of C<print>, e.g.
pdl> p ones 5,3
[
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
]
=item *
C<q> and C<x> are short-hand for C<quit>.
=item *
C<l> lists the history buffer
pdl> l # list last 20 commands
pdl> l 40 # list last 40 commands
=item *
C<?> is an alias for L<help|PDL::Doc::Perldl/help>
pdl> ? pdl2 # get help for new pdl2 shell
=item *
C<??> is an alias for L<apropos|PDL::Doc::Perldl/apropos>
pdl> ?? PDL::Doc
=item *
L<help|PDL::Doc::Perldl/help>, L<apropos|PDL::Doc::Perldl/apropos>,
L<usage|PDL::Doc::Perldl/usage> and L<sig|PDL::Doc::Perldl/sig>:
all words after these commands are used verbatim and not evaluated
by perl. So you can write, e.g.,
pdl> help help
instead of