This file is indexed.

/usr/lib/perl5/PDL/NiceSlice.pm is in pdl 1:2.007-2build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
BEGIN {
   my %engine_ok = (
      'Filter::Util::Call' => 'PDL/NiceSlice/FilterUtilCall.pm',
      'Filter::Simple'     => 'PDL/NiceSlice/FilterSimple.pm',
      'Module::Compile'     => 'PDL/NiceSlice/ModuleCompile.pm',
   );  # to validate names

   ## $PDL::NiceSlice::engine = $engine_ok{'Filter::Simple'};  # default engine type
   ## TODO: Add configuration argument to perldl.conf
   $PDL::NiceSlice::engine = $engine_ok{'Filter::Util::Call'};  # default engine type

   if ( exists $ENV{PDL_NICESLICE_ENGINE} ) {
      my $engine = $ENV{PDL_NICESLICE_ENGINE};
      if ( exists $engine_ok{$engine} and $engine_ok{$engine} ) {
         $PDL::NiceSlice::engine = $engine_ok{$engine};
         warn "PDL::NiceSlice using engine '$engine'\n" if $PDL::verbose;
      } elsif ( exists $engine_ok{$engine} and not $engine_ok{$engine} ) {
         warn "PDL::NiceSlice using default engine\n" if $PDL::verbose;
      } else {
         die "PDL::NiceSlice: PDL_NICESLICE_ENGINE set to invalid engine '$engine'\n";
      }
   }
}

no warnings;

package PDL::NiceSlice;

our $VERSION = '1.000_003';
$VERSION = eval $VERSION;

$PDL::NiceSlice::debug = defined($PDL::NiceSlice::debug) ? $PDL::NiceSlice::debug : 0;
# replace all occurences of the form
#
#   $pdl(args);
# or
#   $pdl->(args);
# with
#
#   $pdl->slice(processed_args);
#
#
# Modified 2-Oct-2001: don't modify $var(LIST) if it's part of a
# "for $var(LIST)" or "foreach $var(LIST)" statement.  CED.
#
# Modified 5-Nov-2007: stop processing if we encounter m/^no\s+PDL\;:\;:NiceSlice\;\s*$/.

# the next one is largely stolen from Regexp::Common
my $RE_cmt = qr'(?:(?:\#)(?:[^\n]*)(?:\n))';

require PDL::Version; # get PDL version number
# 
# remove code for PDL versions earlier than 2.3
# 

use Text::Balanced; # used to find parenthesis-delimited blocks 

# Try overriding the current extract_quotelike() routine
# needed before using Filter::Simple to work around a bug
# between Text::Balanced and Filter::Simple for our purpose.
#

BEGIN {

   no warnings;  # quiet warnings for this

   sub Text::Balanced::extract_quotelike (;$$)
   {
      my $textref = $_[0] ? \$_[0] : \$_;
      my $wantarray = wantarray;
      my $pre  = defined $_[1] ? $_[1] : '\s*';
   
      my @match = Text::Balanced::_match_quotelike($textref,$pre,0,0);        # do not match // alone as m//
      return Text::Balanced::_fail($wantarray, $textref) unless @match;
      return Text::Balanced::_succeed($wantarray, $textref,
                      $match[2], $match[18]-$match[2],        # MATCH
                      @match[18,19],                          # REMAINDER
                      @match[0,1],                            # PREFIX
                      @match[2..17],                          # THE BITS
                      @match[20,21],                          # ANY FILLET?
                     );
   };

};


# a call stack for error processing
my @callstack = ('stackbottom');
sub curarg {
  my $arg = $callstack[-1]; # return top element of stack
  $arg =~ s/\((.*)\)/$1/s;
  return $arg;
}
sub savearg ($) {push @callstack,$_[0]}
sub poparg () {pop @callstack}

my @srcstr = (); # stack for refs to current source strings
my $offset = 1;  # line offset
my $file   = 'unknown';

my $mypostfix = '';

sub autosever {
  my ($this,$arg) = @_;
  $arg = 1 unless defined $arg;
  if ($arg) {$mypostfix = '->sever'} else
    {$mypostfix = ''}
}

sub line {
  die __PACKAGE__." internal error: can't determine line number"
    if $#srcstr < 0;
  my $pretext = substr ${$srcstr[0]}, 0, pos(${$srcstr[0]})-1;
  return ($pretext =~ tr/\n/\n/)+$offset;
}

sub filterdie {
  my ($msg) = @_;
  die "$msg\n\t at $file near line ".
    line().", slice expression '".curarg()."'\n";
}

# non-bracketed prefix matching regexp
my $prebrackreg = qr/^([^\(\{\[]*)/;

# split regex $re separated arglist
# but ignore bracket-protected bits
# (i.e. text that is within matched brackets)
sub splitprotected ($$) {
  my ($re,$txt) = @_;
  my ($got,$pre) = (1,'');
  my @chunks = ('');
  my $ct = 0; # infinite loop protection
  while ($got && $txt =~ /[({\[]/ && $ct++ < 1000) {
    # print "iteration $ct\n";
    ($got,$txt,$pre) =
      Text::Balanced::extract_bracketed($txt,'{}()[]',$prebrackreg);
    my @partialargs = split $re, $pre, -1;
    $chunks[-1] .= shift @partialargs if @partialargs;
    push @chunks, @partialargs;
    $chunks[-1] .= $got;
  }
  filterdie "possible infinite parse loop, slice arg '".curarg()."'"
			   if $ct == 1000;
  my @partialargs = split $re, $txt, -1;
  $chunks[-1] .= shift @partialargs if @partialargs;
  push @chunks, @partialargs;
  return @chunks;
}

# a pattern that finds occurences of the form
#
#  $var(
#
# and
#
#  ->(
#
# used as the prefix pattern for findslice
my $prefixpat = qr/.*?  # arbitrary leading stuff
                   ((?<!&)\$\w+  # $varname not preceded by '&'
                    |->)         # or just '->'
                    (\s|$RE_cmt)* # ignore comments
		    \s*          # more whitespace
                   (?=\()/smx;   # directly followed by open '(' (look ahead)

# translates a single arg into corresponding slice format
sub onearg ($) {
  my ($arg) = @_;
  print STDERR "processing arg '$arg'\n" if $PDL::NiceSlice::debug;
  return q|'X'| if $arg =~ /^\s*:??\s*$/;     # empty arg or just colon
  # recursively process args for slice syntax
  $arg = findslice($arg,$PDL::debug) if $arg =~ $prefixpat;
  # no doubles colon are matched to avoid confusion with Perl's C<::>
  if ($arg =~ /(?<!:):(?!:)/) { # a start:stop:delta range
    my @args = splitprotected '(?<!:):(?!:)', $arg;
    filterdie "invalid range in slice expression '".curarg()."'"
      if @args > 3;
    $args[0] = 0 if !defined $args[0] || $args[0] =~ /^\s*$/;
    $args[1] = -1 if !defined $args[1] || $args[1] =~ /^\s*$/;
    $args[2] = undef if !defined $args[2] || $args[2] =~ /^\s*$/;
    return "[".join(',',@args)."]"; # replace single ':' with ','
  }
  # the (pos) syntax, i.e. 0D slice
  return "[$arg,0,0]" if $arg =~ s/^\s*\((.*)\)\s*$/$1/; # use the new [x,x,0]
  # we don't allow [] syntax (although that's what slice uses)
  filterdie "invalid slice expression containing '[', expression was '".
    curarg()."'" if $arg =~ /^\s*\[/;

  # If the arg starts with '*' it's a dummy call -- force stringification
  # and prepend a '*' for handling by slice.
  return "(q(*).($arg))" if($arg =~ s/^\s*\*//);

  # this must be a simple position, leave as is
  return "$arg";
}

# process the arg list
sub procargs {
  my ($txt) = @_;
  print STDERR "procargs: got '$txt'\n" if $PDL::NiceSlice::debug;
  # $txt =~ s/^\s*\((.*)\)\s*$/$1/s; # this is now done by findslice
  # push @callstack, $txt; # for later error reporting
  my $args = $txt =~ /^\s*$/s ? '' :
    join ',', map {onearg $_} splitprotected ',', $txt;
    ## Leave whitespace/newlines in so line count
    ## is preserved in error messages.  Makes the
    ## filtered output ugly---iffi the input was
    ## ugly...
    ## 
    ## $args =~ s/\s//sg; # get rid of whitespace
  # pop @callstack; # remove from call stack
  print STDERR "procargs: returned '($args)'\n" if $PDL::NiceSlice::debug;
  return "($args)";
}

# this is the real workhorse that translates occurences
# of $a(args) into $args->slice(processed_arglist)
#
sub findslice {
  my ($src,$verb) = @_;
  push @srcstr, \$src;
  $verb = 0 unless defined $verb;
  my $processed = '';
  my $ct=0; # protect against infinite loop
  my ($found,$prefix,$dummy);
  while ( $src =~ m/\G($prefixpat)/ && (($found,$dummy,$prefix) =
	   Text::Balanced::extract_bracketed($src,'()',$prefixpat))[0]
	  && $ct++ < 1000) {
    print STDERR "pass $ct: found slice expr $found at line ".line()."\n"
      if $verb;

#  Do final check for "for $var(LIST)" and "foreach $var(LIST)" syntax. 
#  Process into an 'slice' call only if it's not that.

    if ($prefix =~ m/for(each)?(\s+(my|our))?\s+\$\w+(\s|$RE_cmt)*$/s ||
      # foreach statement: Don't translate
	$prefix =~ m/->\s*\$\w+$/s) # e.g. $a->$method(args)
      # method invocation via string, don't translate either
    {
	# note: even though we reject this one we need to call
        #       findslice on $found in case
	#       it contains slice expressions
      $processed .= "$prefix".findslice($found);
    } else {      # statement is a real slice and not a foreach

      my ($call,$pre,$post,$arg);

      # the following section got an overhaul in v0.99
      # to fix modifier parsing and allow >1 modifier
      # this code still needs polishing
      savearg $found; # error reporting
      print STDERR "findslice: found '$found'\n" if $PDL::NiceSlice::debug;
      $found =~ s/^\s*\((.*)\)\s*$/$1/s;
      my ($slicearg,@mods) = splitprotected ';', $found;
      filterdie "more than 1 modifier group: @mods" if @mods > 1;
      # filterdie "invalid modifier $1"
      #	if $found =~ /(;\s*[[:graph:]]{2,}?\s*)\)$/;
      print STDERR "MODS: " . join(',',@mods) . "\n" if $PDL::NiceSlice::debug;
      my @post = (); # collects all post slice operations
      my @pre = ();
      if (@mods) {
	(my $mod = $mods[0]) =~ s/\s//sg; # eliminate whitespace
	my @modflags = split '', $mod;
	print STDERR "MODFLAGS: @modflags\n" if $PDL::NiceSlice::debug;
	filterdie "more than 1 modifier incompatible with ?: @modflags"
	  if @modflags > 1 && grep (/\?/, @modflags); # only one flag with where
	my %seen = ();
	if (@modflags) {
	  for my $mod1 (@modflags) {
	    if ($mod1 eq '?') {
	      $seen{$mod1}++ && filterdie "modifier $mod1 used twice or more";
	      $call = 'where';
	      $arg = "(" . findslice($slicearg) . ")";
	      # $post = ''; # no post action required
	    } elsif ($mod1 eq '_') {
	      $seen{$mod1}++ && filterdie "modifier $mod1 used twice or more";
	      push @pre, 'flat->';
	      $call ||= 'slice';       # do only once
	      $arg = procargs($slicearg);
	      # $post = ''; # no post action required
	    } elsif ($mod1 eq '|') {
	      $seen{$mod1}++ && filterdie "modifier $mod1 used twice or more";
	      $call ||= 'slice';
	      $arg ||= procargs($slicearg);
	      push @post, '->sever';
	    } elsif ($mod1 eq '-') {
	      $seen{$mod1}++ && filterdie "modifier $mod1 used twice or more";
	      $call ||= 'slice';
	      $arg ||= procargs($slicearg);
	      push @post, '->reshape(-1)';
	    } else {
	      filterdie "unknown modifier $mod1";
	    }
	  }
	} else { # empty modifier block
	  $call = 'slice';
	  $arg = procargs($slicearg);
	  # $post = '';
	}
      } else { # no modifier block
         $call = 'slice';
         $arg = procargs($slicearg);
         # $post = '';
         # $call = 'slice_if_pdl';     # handle runtime checks for $self type
         # $arg =~ s/\)$/,q{$found})/;  # add original argument string
                                        # in case $self is not a piddle
                                        # and the original call must be
                                        # generated
      }
      $pre = join '', @pre;
      # assumption here: sever should be last
      # and order of other modifiers doesn't matter
      $post = join '', sort @post; # need to ensure that sever is last
      $processed .= "$prefix". ($prefix =~ /->(\s*$RE_cmt*)*$/ ? 
				'' : '->').
	$pre.$call.$arg.$post.$mypostfix;
    }

  } # end of while loop

  poparg;      # clean stack
  pop @srcstr; # clear stack
  # append the remaining text portion
  #     use substr only if we have had at least one pass
  #     through above loop (otherwise pos is uninitialized)
  $processed .= $ct > 0 ? substr $src, pos($src) : $src;
}

##############################
# termstr - generate a regexp to find turn-me-off strings
# CED 5-Nov-2007
sub terminator_regexp{
    my $clstr = shift;
    $clstr =~ s/([^a-zA-Z0-9])/\\$1/g;
    my $termstr = '^\s*no\s+'.$clstr.'\s*;\s*(#.*)*$';
    return qr/$termstr/o; # allow trailing comments
}

sub reinstator_regexp{
    my $clstr = shift;
    $clstr =~ s/([^a-zA-Z0-9])/\\$1/g;
    my $reinstr = '^\s*use\s+'.$clstr.'\s*;\s*(#.*)*$';
    return qr/$reinstr/o; # allow trailing comments
}

# save eval of findslice that should be used within perldl or pdl2
# as a preprocessor
sub perldlpp {
 my ($class, $txt) = @_;
 local($_);
 ##############################
 # Backwards compatibility to before the two-parameter form. The only
 # call should be around line 206 of PDL::AutoLoader, but one never
 # knows....
 #    -- CED 5-Nov-2007
 if(!defined($txt)) { 
     print "PDL::NiceSlice::perldlpp -- got deprecated one-argument form, from ".(join("; ",caller))."...\n";
     $txt = $class; 
     $class = "PDL::NiceSlice";
 }

 ## Debugging to track exactly what is going on -- left in, in case it's needed again
 if($PDL::debug > 1) {
     print "PDL::NiceSlice::perldlpp - got:\n$txt\n";
     my $i;
     for $i(0..5){
	 my($package,$filename,$line,$subroutine, $hasargs) = caller($i);
	 printf("layer %d: %20s, %40s, line %5d, sub %20s, args: %d\n",$i,$package,$filename,$line,$subroutine,$hasargs);
     }
 }

 my $new;

 ##############################
 ## This block sort-of echoes import(), below...
 ## Crucial difference: we don't give up the ghost on termination conditions, only
 ## mask out current findslices.  That's because future uses won't be processed
 ## (for some reason source filters don't work on evals).

 my @lines= split /\n/,$txt;

 my $terminator = terminator_regexp($class);
 my $reinstator = reinstator_regexp($class);

 my($status, $off, $end);
 eval {
     do {
	 my $data = "";
	 while(@lines) {
	     $_= shift @lines;
	     if(defined($terminator) && m/$terminator/) {
		 $_ = "## $_";
		 $off = 1;
		 last;
	     }
	     if(defined($reinstator) && m/$reinstator/) {
		 $_ = "## $_";
	     }
	     if(m/^\s*(__END__|__DATA__)\s*$/) {
		 $end=$1; $off = 1;
		 last;
	     }
	     $data .= "$_\n";
	     $count++;
	     $_="";
	 }
	 $_ = $data;
	 $_ = findslice $_ ;
	 $_ .= "no $class;\n" if $off;
	 $_ .= "$end\n" if $end;
	 $new .= "$_";
	 
	 while($off && @lines) {
	     $_ = shift @lines;
	     if(defined($reinstator) && m/$reinstator/) {
		 $off = 0;
		 $_ = "## $_";
	     }
	     if(defined($terminator) && m/$terminator/) {
		 $_ = "## $_";
	     }

	     $new .= "$_\n";

	 }
     } while(@lines && !$end);
 };
     
 if ($@) {
   my $err = $@;
   for (split '','#!|\'"%~/') {
     return "print q${_}NiceSlice error: $err${_}"
       unless $err =~ m{[$_]};
    }
   return "print q{NiceSlice error: $err}"; # if this doesn't work
                                               # we're stuffed
 }

 if($PDL::debug > 1) {
     print "PDL::NiceSlice::perldlpp - returning:\n$new\n";
 }
 return $new;
}

BEGIN {
   require "$PDL::NiceSlice::engine";
}

=head1 NAME

PDL::NiceSlice - toward a nicer slicing syntax for PDL

=head1 SYNOPSYS

  use PDL::NiceSlice;

  $a(1:4) .= 2;             # concise syntax for ranges
  print $b((0),1:$end);     # use variables in the slice expression
  $a->xchg(0,1)->(($pos-1)) .= 0; # default method syntax

  $idx = long 1, 7, 3, 0;   # a piddle of indices
  $a(-3:2:2,$idx) += 3;     # mix explicit indexing and ranges
  $a->clump(1,2)->(0:30);   # 'default method' syntax
  $a(myfunc(0,$var),1:4)++; # when using functions in slice expressions
                            # use parentheses around args!

  $b = $a(*3);              # Add dummy dimension of order 3

  # modifiers are specified in a ;-separated trailing block
  $a($a!=3;?)++;            # short for $a->where($a!=3)++
  $a(0:1114;_) .= 0;        # short for $a->flat->(0:1114)
  $b = $a(0:-1:3;|);        # short for $a(0:-1:3)->sever
  $n = sequence 3,1,4,1;
  $b = $n(;-);              # drop all dimensions of size 1 (AKA squeeze)
  $b = $n(0,0;-|);          # squeeze *and* sever
  $c = $a(0,3,0;-);         # more compact way of saying $a((0),(3),(0))

=head1 DESCRIPTION

Slicing is a basic, extremely common operation, and PDL's
L<slice|PDL::Slices/slice> method would be cumbersome to use in many
cases.  C<PDL::NiceSlice> rectifies that by incorporating new slicing
syntax directly into the language via a perl I<source filter> (see
L<the perlfilter man page|perlfilter>).  NiceSlice adds no new functionality, only convenient syntax.

NiceSlice is loaded automatically in the perldl or pdl2 shell, but (to avoid
conflicts with other modules) must be loaded explicitly in standalone
perl/PDL scripts (see below).  If you prefer not to use a prefilter on
your standalone scripts, you can use the L<slice|PDL::Slices/slice>
method in those scripts,
rather than the more compact NiceSlice constructs.

=head1 Use in scripts and C<perldl> or C<pdl2> shell

The new slicing syntax can be switched on and off in scripts
and perl modules by using or unloading C<PDL::NiceSlice>.

But now back to scripts and modules.
Everything after C<use PDL::NiceSlice> will be translated
and you can use the new slicing syntax. Source filtering
will continue until the end of the file is encountered.
You can stop sourcefiltering before the end of the file
by issuing a C<no PDL::NiceSlice> statement.

Here is an example:

  use PDL::NiceSlice;

  # this code will be translated
  # and you can use the new slicing syntax

  no PDL::NiceSlice;

  # this code won't
  # and the new slicing syntax will raise errors!

See also L<Filter::Simple> and F<example> in this distribution for
further examples.

NOTE: Unlike "normal" modules you need to include a
C<use PDL::NiceSlice> call in each and every file that
contains code that uses the new slicing syntax. Imagine
the following situation: a file F<test0.pl>

   # start test0.pl
   use PDL;
   use PDL::NiceSlice;

   $a = sequence 10;
   print $a(0:4),"\n";

   require 'test1.pl';
   # end test0.pl

that C<require>s a second file F<test1.pl>

   # begin test1.pl
   $aa = sequence 11;
   print $aa(0:7),"\n";
   1;
   # end test1.pl

Following conventional perl wisdom everything should be alright
since we C<use>d C<PDL> and C<PDL::NiceSlice> already from within
F<test0.pl> and by the time F<test1.pl> is C<require>d things should
be defined and imported, etc. A quick test run will, however, produce
something like the following:

  perl test0.pl
 [0 1 2 3 4]
 syntax error at test1.pl line 3, near "0:"
 Compilation failed in require at test0.pl line 7.

This can be fixed by adding the line

  use PDL::NiceSlice;

C<before> the code in F<test1.pl> that uses the
new slicing syntax (to play safe just include the line
near the top of the file), e.g.

   # begin corrected test1.pl
   use PDL::NiceSlice;
   $aa = sequence 11;
   print $aa(0:7),"\n";
   1;
   # end test1.pl

Now things proceed more smoothly

  perl test0.pl
 [0 1 2 3 4]
 [0 1 2 3 4 5 6 7]

Note that we don't need to issue C<use PDL> again.
C<PDL::NiceSlice> is a somewhat I<funny> module in
that respect. It is a consequence of the way source
filtering works in Perl (see also the IMPLEMENTATION
section below).

=head2 evals and C<PDL::NiceSlice>

Due to C<PDL::NiceSlice> being a source filter it won't work
in the usual way within evals. The following will I<not> do what
you want:

  $a = sequence 10;
  eval << 'EOE';

  use PDL::NiceSlice;
  $b = $a(0:5);

  EOE
  print $b;

Instead say:

  use PDL::NiceSlice;
  $a = sequence 10;
  eval << 'EOE';

  $b = $a(0:5);

  EOE
  print $b;

Source filters I<must> be executed at compile time to be effective. And
C<PDL::NiceFilter> is just a source filter (although it is not
necessarily obvious for the casual user).

=head1 The new slicing syntax

Using C<PDL::NiceSlice> slicing piddles becomes so much easier since, first of
all, you don't need to make explicit method calls. No

  $pdl->slice(....);

calls, etc. Instead, C<PDL::NiceSlice> introduces two ways in which to
slice piddles without too much typing:

=over 2

=item *

using parentheses directly following a scalar variable name,
for example

   $c = $b(0:-3:4,(0));

=item *

using the so called I<default method> invocation in which the
piddle object is treated as if it were a reference to a
subroutine (see also L<perlref>). Take this example that slices
a piddle that is part of a perl list C<@b>:

  $c = $b[0]->(0:-3:4,(0));

=back

The format of the argument list is the same for both types of
invocation and will be explained in more detail below.

=head2 Parentheses following a scalar variable name

An arglist in parentheses following directly after a scalar variable
name that is I<not> preceded by C<&> will be resolved as a slicing
command, e.g.

  $a(1:4) .= 2;         # only use this syntax on piddles
  $sum += $a(,(1));

However, if the variable name is immediately preceded by a C<&>,
for example

  &$a(4,5);

it will not be interpreted as a slicing expression. Rather, to avoid
interfering with the current subref syntax, it will be treated as an
invocation of the code reference C<$a> with argumentlist C<(4,5)>.

The $a(ARGS) syntax collides in a minor way with the perl syntax.  In
particular, ``foreach $var(LIST)'' appears like a PDL slicing call.  
NiceSlice avoids translating the ``for $var(LIST)'' and 
``foreach $var(LIST)'' constructs for this reason.  Since you
can't use just any old lvalue expression in the 'foreach' 'for'
constructs -- only a real perl scalar will do -- there's no 
functionality lost.  If later versions of perl accept 
``foreach <lvalue-expr> (LIST)'', then you can use the code ref
syntax, below, to get what you want.

=head2 The I<default method> syntax

The second syntax that will be recognized is what I called the
I<default method> syntax. It is the method arrow C<-E<gt>> directly
followed by an open parenthesis, e.g.

  $a->xchg(0,1)->(($pos)) .= 0;

Note that this conflicts with the use of normal code references, since you
can write in plain Perl

  $sub = sub { print join ',', @_ };
  $sub->(1,'a');

NOTE: Once C<use PDL::NiceSlice> is in effect (you can always switch it off with
a line C<no PDL::NiceSlice;> anywhere in the script) the source filter will incorrectly
replace the above call to C<$sub> with an invocation of the slicing method.
This is one of the pitfalls of using a source filter that doesn't know
anything about the runtime type of a variable (cf. the
Implementation section).

This shouldn't be a major problem in practice; a simple workaround is to use
the C<&>-way of calling subrefs, e.g.:

  $sub = sub { print join ',', @_ };
  &$sub(1,'a');

=head2 When to use which syntax?

Why are there two different ways to invoke slicing?
The first syntax C<$a(args)> doesn't work with chained method calls. E.g.

  $a->xchg(0,1)(0);

won't work. It can I<only> be used directly following a valid perl variable
name. Instead, use the I<default method> syntax in such cases:

  $a->xchg(0,1)->(0);

Similarly, if you have a list of piddles C<@pdls>:

  $b = $pdls[5]->(0:-1);

=head2 The argument list

The argument list is a comma separated list. Each argument specifies
how the corresponding dimension in the piddle is sliced. In contrast
to usage of the L<slice|PDL::Slices/slice> method the arguments should
I<not> be quoted. Rather freely mix literals (1,3,etc), perl
variables and function invocations, e.g.

  $a($pos-1:$end,myfunc(1,3)) .= 5;

There can even be other slicing commands in the arglist:

  $a(0:-1:$pdl($step)) *= 2;

NOTE: If you use function calls in the arglist make sure that
you use parentheses around their argument lists. Otherwise the
source filter will get confused since it splits the argument
list on commas that are not protected by parentheses. Take
the following example:

  sub myfunc { return 5*$_[0]+$_[1] }
  $a = sequence 10;
  $sl = $a(0:myfunc 1, 2);
  print $sl;
 PDL barfed: Error in slice:Too many dims in slice
 Caught at file /usr/local/bin/perldl, line 232, pkg main


The simple fix is

  $sl = $a(0:myfunc(1, 2));
  print $sl;
 [0 1 2 3 4 5 6 7]

Note that using prototypes in the definition of myfunc does not help.
At this stage the source filter is simply not intelligent enough to
make use of this information. So beware of this subtlety.

Another pitfall to be aware of: currently, you can't use the conditional
operator in slice expressions (i.e., C<?:>, since the parser confuses them
with ranges). For example, the following will cause an error:

  $a = sequence 10;
  $b = rand > 0.5 ? 0 : 1; # this one is ok
  print $a($b ? 1 : 2);    # error !
 syntax error at (eval 59) line 3, near "1,

For the moment, just try to stay clear of the conditional operator
in slice expressions (or provide us with a patch to the parser to
resolve this issue ;).

=head2 Modifiers

Following a suggestion originally put forward by Karl Glazebrook the
latest versions of C<PDL::NiceSlice> implement I<modifiers> in slice
expressions. Modifiers are convenient shorthands for common variations
on PDL slicing. The general syntax is

    $pdl(<slice>;<modifier>)

Four modifiers are currently implemented:

=over

=item *

C<_> : I<flatten> the piddle before applying the slice expression. Here
is an example

   $b = sequence 3, 3;
   print $b(0:-2;_); # same as $b->flat->(0:-2)
 [0 1 2 3 4 5 6 7]

which is quite different from the same slice expression without the modifier

   print $b(0:-2);
 [
  [0 1]
  [3 4]
  [6 7]
 ]

=item *

C<|> : L<sever|PDL::Core/sever> the link to the piddle, e.g.

   $a = sequence 10;
   $b = $a(0:2;|)++;  # same as $a(0:2)->sever++
   print $b;
 [1 2 3]
   print $a; # check if $a has been modified
 [0 1 2 3 4 5 6 7 8 9]

=item *

C<?> : short hand to indicate that this is really a
L<where|PDL::Primitive/where> expression

As expressions like

  $a->where($a>5)

are used very often you can write that shorter as

  $a($a>5;?)

With the C<?>-modifier the expression preceding the modifier is I<not>
really a slice expression (e.g. ranges are not allowed) but rather an
expression as required by the L<where|PDL::Primitive/where> method.
For example, the following code will raise an error:

  $a = sequence 10;
  print $a(0:3;?);
 syntax error at (eval 70) line 3, near "0:"

That's about all there is to know about this one.

=item *

C<-> : I<squeeze> out any singleton dimensions. In less technical terms:
reduce the number of dimensions (potentially) by deleting all
dims of size 1. It is equivalent to doing a L<reshape|PDL::Core/reshape>(-1).
That can be very handy if you want to simplify
the results of slicing operations:

  $a = ones 3, 4, 5;
  $b = $a(1,0;-); # easier to type than $a((1),(0))
  print $b->info;
 PDL: Double D [5]

It also provides a unique opportunity to have smileys in your code!
Yes, PDL gives new meaning to smileys.

=back

=head2 Combining modifiers

Several modifiers can be used in the same expression, e.g.

  $c = $a(0;-|); # squeeze and sever

Other combinations are just as useful, e.g. C<;_|> to flatten and
sever. The sequence in which modifiers are specified is not important.

A notable exception is the C<where> modifier (C<?>) which must not
be combined with other flags (let me know if you see a good reason
to relax this rule).

Repeating any modifier will raise an error:

  $c = $a(-1:1;|-|); # will cause error
 NiceSlice error: modifier | used twice or more

Modifiers are still a new and experimental feature of
C<PDL::NiceSlice>. I am not sure how many of you are actively using
them. I<Please do so and experiment with the syntax>. I think
modifiers are very useful and make life a lot easier.  Feedback is
welcome as usual. The modifier syntax will likely be further tuned in
the future but we will attempt to ensure backwards compatibility
whenever possible.

=head2 Argument formats

In slice expressions you can use ranges and secondly,
piddles as 1D index lists (although compare the description
of the C<?>-modifier above for an exception).

=over 2

=item * ranges

You can access ranges using the usual C<:> separated format:

  $a($start:$stop:$step) *= 4;

Note that you can omit the trailing step which then defaults to 1.  Double
colons (C<::>) are not allowed to avoid clashes with Perl's namespace
syntax. So if you want to use steps different from the default
you have to also at least specify the stop position.
Examples:

  $a(::2);   # this won't work (in the way you probably intended)
  $a(:-1:2); # this will select every 2nd element in the 1st dim

Just as with L<slice|PDL::Slices/slice> negative indices count from the end of the dimension
backwards with C<-1> being the last element. If the start index is larger
than the stop index the resulting piddle will have the elements in reverse
order between these limits:

  print $a(-2:0:2);
 [8 6 4 2 0]

A single index just selects the given index in the slice

  print $a(5);
 [5]

Note, however, that the corresponding dimension is not removed from
the resulting piddle but rather reduced to size 1:

  print $a(5)->info
 PDL: Double D [1]

If you want to get completely rid of that dimension enclose the index
in parentheses (again similar to the L<slice|PDL::Slices/slice> syntax):

  print $a((5));
 5

In this particular example a 0D piddle results. Note that this syntax is
only allowed with a single index. All these will be errors:

  print $a((0,4));  # will work but not in the intended way
  print $a((0:4));  # compile time error

An empty argument selects the whole dimension, in this example
all of the first dimension:

  print $a(,(0));

Alternative ways to select a whole dimension are

  $a = sequence 5, 5; 
  print $a(:,(0));
  print $a(0:-1,(0));
  print $a(:-1,(0));
  print $a(0:,(0));

Arguments for trailing dimensions can be omitted. In that case
these dimensions will be fully kept in the sliced piddle:

  $a = random 3,4,5;
  print $a->info;
 PDL: Double D [3,4,5]
  print $a((0))->info;
 PDL: Double D [4,5]
  print $a((0),:,:)->info;  # a more explicit way
 PDL: Double D [4,5]
  print $a((0),,)->info;    # similar
 PDL: Double D [4,5]

=item * dummy dimensions

As in L<slice|slice>, you can insert a dummy dimension by preceding a
single index argument with '*'.  A lone '*' inserts a dummy dimension of 
order 1; a '*' followed by a number inserts a dummy dimension of that order.

=item * piddle index lists

The second way to select indices from a dimension is via 1D piddles
of indices. A simple example:

  $a = random 10;
  $idx = long 3,4,7,0;
  $b = $a($idx);

This way of selecting indices was previously only possible using
L<dice|PDL::Slices/dice> (C<PDL::NiceSlice> attempts to unify the
C<slice> and C<dice> interfaces). Note that the indexing piddles must
be 1D or 0D. Higher dimensional piddles as indices will raise an error:

  $a = sequence 5, 5;
  $idx2 = ones 2,2;
  $sum = $a($idx2)->sum;
 piddle must be <= 1D at /home/XXXX/.perldlrc line 93

Note that using index piddles is not as efficient as using ranges.
If you can represent the indices you want to select using a range
use that rather than an equivalent index piddle. In particular,
memory requirements are increased with index piddles (and execution
time I<may> be longer). That said, if an index piddle is the way to
go use it!

=back

As you might have expected ranges and index piddles can be freely
mixed in slicing expressions:

  $a = random 5, 5;
  $b = $a(-1:2,pdl(3,0,1));

=head2 piddles as indices in ranges

You can use piddles to specify indices in ranges. No need to
turn them into proper perl scalars with the new slicing syntax.
However, make sure they contain not more than one element! Otherwise
a runtime error will be triggered. First a couple of examples that
illustrate proper usage:

  $a = sequence 5, 5;
  $rg = pdl(1,-1,3);
  print $a($rg(0):$rg(1):$rg(2),2);
 [
  [11 14]
 ]
  print $a($rg+1,:$rg(0));
 [
  [2 0 4]
  [7 5 9]
 ]

The next one raises an error 

  print $a($rg+1,:$rg(0:1));
 multielement piddle where only one allowed at XXX/Core.pm line 1170.

The problem is caused by using the 2-element piddle C<$rg(0:1)> as the
stop index in the second argument C<:$rg(0:1)> that is interpreted as
a range by C<PDL::NiceSlice>. You I<can> use multielement piddles as
index piddles as described above but not in ranges. And
C<PDL::NiceSlice> treats any expression with unprotected C<:>'s as a
range.  I<Unprotected> means as usual 
I<"not occurring between matched parentheses">.

=head1 IMPLEMENTATION

C<PDL::NiceSlice> exploits the ability of Perl to use source filtering
(see also L<perlfilter>). A source filter basically filters (or
rewrites) your perl code before it is seen by the
compiler. C<PDL::NiceSlice> searches through your Perl source code and when
it finds the new slicing syntax it rewrites the argument list
appropriately and splices a call to the C<slice> method using the
modified arg list into your perl code. You can see how this works in
the L<perldl|perldl> or L<pdl2|PDL::Perldl2> shells by switching on
reporting (see above how to do that).

=head1 BUGS

=head2 Conditional operator

The conditional operator can't be used in slice expressions (see
above).

=head2 The C<DATA> file handle

I<Note>: To avoid clobbering the C<DATA> filehandle C<PDL::NiceSlice>
switches itself off when encountering the C<__END__> or C<__DATA__> tokens.
This should not be a problem for you unless you use C<SelfLoader> to load
PDL code including the new slicing from that section. It is even desirable
when working with L<Inline::Pdlpp|Inline::Pdlpp>, see below.

=head2 Possible interaction with L<Inline::Pdlpp|Inline::Pdlpp>

There is currently an undesired interaction between C<PDL::NiceSlice>
and the new L<Inline::Pdlpp|Inline::Pdlpp> module (currently only in 
PDL CVS). Since PP code generally
contains expressions of the type C<$var()> (to access piddles, etc)
C<PDL::NiceSlice> recognizes those I<incorrectly> as
slice expressions and does its substitutions. This is not a problem
if you use the C<DATA> section for your Pdlpp code -- the recommended
place for Inline code anyway. In that case
C<PDL::NiceSlice> will have switched itself off before encountering any
Pdlpp code (see above):

    # use with Inline modules
  use PDL;
  use PDL::NiceSlice;
  use Inline Pdlpp;

  $a = sequence(10);
  print $a(0:5);

  __END__

  __Pdlpp__

  ... inline stuff


Otherwise switch C<PDL::NiceSlice> explicitly off around the
Inline::Pdlpp code:

  use PDL::NiceSlice;

  $a = sequence 10;
  $a(0:3)++;
  $a->inc;

  no PDL::NiceSlice; # switch off before Pdlpp code
  use Inline Pdlpp => "Pdlpp source code";

The cleaner solution is to always stick with the
C<DATA> way of including your C<Inline> code as
in the first example. That way you keep your nice Perl
code at the top and all the ugly Pdlpp stuff etc at
the bottom.

=head2 Bug reports

Feedback and bug reports are welcome. Please include an example
that demonstrates the problem. Log bug reports in the PDL
bug database at

  http://sourceforge.net/bugs/?group_id=612

or send them to the pdl-porters mailing list
E<lt>pdl-porters@jach.hawaii.eduE<gt>.


=head1 COPYRIGHT

Copyright (c) 2001, 2002 Christian Soeller. All Rights Reserved.
This module is free software. It may be used, redistributed
and/or modified under the same terms as PDL itself
(see L<http://pdl.perl.org>).

=cut

1;