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


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


  use PDL::NiceSlice;
                            # use parentheses around args!
  # modifiers are specified in a ;-separated trailing block
  $n = sequence 3,1,4,1;
  # Use with perldl versions < v1.31 (or include these lines in .perldlrc)
  perldl> use PDL::NiceSlice;
  # next one is required, see below
  perldl> $PERLDL::PREPROCESS = \&PDL::NiceSlice::perldlpp;
  perldl> $a(4:5) .= xvals(2);


PDL's slice syntax sucks. This module tries to rectify the situation to some degree. Using Perl's ability to use source filtering (which you can think of as a very powerful macro facility, see the perlfilter manpage) it introduces a more reasonable syntax for slicing PDL objects (AKA piddles). PDL::NiceSlice implements the source filter that provides this new feature.

NOTE: PDL::NiceSlice relies on several modules from CPAN that make source filtering and parsing easier. In particular it requires the Filter distribution and Text::Balanced. To make your life easier it is recommended that you use the CPAN module to resolve dependencies during installation automatically. Using the cpan installation shell installation should be as easy as this:

   $ perl -MCPAN -e shell
   cpan> install PDL::NiceSlice

Use in scripts and perldl shell


Note: this will not work in the perldl shell < v1.31. Because the perldl shell uses evals, and NiceSlice is a perl source filter, you have to set a special variable to use it within perldl. See below how to enable the new slicing syntax within older perldl.

But now back to scripts and modules. Everything after use PDL::NiceSlice will be translated and you can use the snew 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 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!


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

   # start test0.pl
   use PDL;
   use PDL::NiceSlice;
   $a = sequence 10;
   require 'test1.pl';
   # end test0.pl

that requires a second file test1.pl

   # begin test1.pl
   $aa = sequence 11;
   # end test1.pl


  perl test0.pl
 [0 1 2 3 4]
 Compilation failed in require at test0.pl line 7.

This can be fixed by adding the line

  use PDL::NiceSlice;


   # begin corrected test1.pl
   use PDL::NiceSlice;
   $aa = sequence 11;
   # end test1.pl

Now things proceed more smoothly

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


Usage with perldl

NOTE: This information only applies to versions of perldl earlier than 1.31 . From v1.31 onwards niceslicing is enabled by default, i.e. it should just work. See perldl for details.

For pre v1.31 perldls you need to add the following two lines to your .perldlrc file:

   use PDL::NiceSlice;

A more complete tool box of commands for experimentation is in the file local.perldlrc in the PDL::NiceSlice source directory. Just include the code in that file in your usual ~/.perldlrc and you can switch source filtering with PDL::NiceSlice on and off by typing trans and notrans, respectively. To see what and how your commands are translated switch reporting on:

  perldl> report 1;

Similarly, switch reporting off as needed

  perldl> report 0;

Note that these commands will only work if you included the contents of local.perldlrc in your perldl startup file. In perldl v1.31 and later these commands are available by default.

evals and PDL::NiceSlice


  $a = sequence 10;
  eval << 'EOE';
  use PDL::NiceSlice;
  $b = $a(0:5);
  print $b;

Instead say:

  use PDL::NiceSlice;
  $a = sequence 10;
  eval << 'EOE';
  $b = $a(0:5);
  print $b;


The new slicing syntax





Parentheses following a scalar variable name






The $a(ARGS) syntax collides in a minor way with the perl syntax. In particular, ``foreach $avar(LIST)'' appears like a PDL slicing call. NiceSlice avoids translating the ``for $avar(LIST)'' and ``foreach $avar(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.

The default method syntax








When to use which syntax?





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


The argument list



There can even be other slicing commands in the arglist:



  $a = sequence 10;
  print $sl;
 PDL barfed: Error in slice:Too many dims in slice

The simple fix is

  print $sl;
 [0 1 2 3 4 5 6 7]



  $a = sequence 10;





Four modifiers are currently implemented:

Combining modifiers

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




Repeating any modifier will raise an error:

 NiceSlice error: modifier | used twice or more


Argument formats



  $a = random 5, 5;

piddles as indices in ranges


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

The next one raises an error




PDL::NiceSlice exploits the ability of Perl to use source filtering (see also the perlfilter manpage). A source filter basically filters (or rewrites) your perl code before it is seen by the compiler. 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 nslice method using the modified arg list into your perl code. You can see how this works in the perldl shell by switching on reporting (see above how to do that).



Conditional operator


The DATA file handle


Possible interaction with Inline::Pdlpp


    # use with Inline modules
  use PDL;
  use PDL::NiceSlice;
  use Inline Pdlpp;
  $a = sequence(10);
  print $a(0:5);
  ... inline stuff


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


Bug reports



or send them to the pdl-porters mailing list <pdl-porters@jach.hawaii.edu>.



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