blib/lib/PDL/NiceSlice.pm

filter: Statementstmt 81.3% 170 / 209 Branchbran 59.8% 79 / 132 Conditioncond 42.8% 27 / 63 MC/DCmcdc 17.9% 7 / 39 Subroutinesub 90.0% 18 / 20 Podpod 7.1% 1 / 14 totaltotal 63.3% 302 / 477 CC 91 SCAR 37.9
CC
91
Cov
68%
perldlpp
123.9
BEGIN
45.9
findslice
36.1
CRAP 44.4
« blib/lib/PDL/IO/Pnm.pm ↑ Summary blib/lib/PDL/Primitive.pm »
1 BEGIN {
2 120 ▶ my %engine_ok = (
Subroutine BEGIN: called CC=8 SCAR=38.3
3 'Filter::Util::Call' => 'PDL/NiceSlice/FilterUtilCall.pm',
4 'Filter::Simple' => 'PDL/NiceSlice/FilterSimple.pm',
5 'Module::Compile' => 'PDL/NiceSlice/ModuleCompile.pm',
6 ); # to validate names
7  
8 ## $PDL::NiceSlice::engine = $engine_ok{'Filter::Simple'}; # default engine type
9 ## TODO: Add configuration argument to perldl.conf
10 120 $PDL::NiceSlice::engine = $engine_ok{'Filter::Util::Call'}; # default engine type
11  
12 120 ▶ if ( exists $ENV{PDL_NICESLICE_ENGINE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"PDL_NICESLICE_ENGINE"}) 0 120 120
13 0 my $engine = $ENV{PDL_NICESLICE_ENGINE};
14 0 ▶ if ( exists $engine_ok{$engine} and $engine_ok{$engine} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $engine_ok{$engine} and $engine_ok{$engine}) { } 0 0 0
elsif (exists $engine_ok{$engine} and not $engine_ok{$engine}) { } 0 0 0
Condition0%0/3
exists $engine_ok{$engine} and $engine_ok{$engine}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $engine_ok{$engine} and $engine_ok{$engine}
exists $engine_ok{$engine} $engine_ok{$engine}
Truth table0%0/3
A and B
A= exists $engine_ok{$engine}B= $engine_ok{$engine}
ABresult
0X0
100
111
15 0 $PDL::NiceSlice::engine = $engine_ok{$engine};
16 0 ▶ warn "PDL::NiceSlice using engine '$engine'\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
17 ▶ } elsif ( exists $engine_ok{$engine} and not $engine_ok{$engine} ) {
Condition0%0/3
exists $engine_ok{$engine} and not $engine_ok{$engine}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $engine_ok{$engine} and not $engine_ok{$engine}
exists $engine_ok{$engine} not $engine_ok{$engine}
Truth table0%0/3
A and not(B)
A= exists $engine_ok{$engine}B= not $engine_ok{$engine}
ABresult
0X0
100
111
18 0 ▶ warn "PDL::NiceSlice using default engine\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
19 } else {
20 0 die "PDL::NiceSlice: PDL_NICESLICE_ENGINE set to invalid engine '$engine'\n";
21 }
22 }
23 }
24  
25 120 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 package PDL::NiceSlice;
28  
29 120 our $VERSION = '1.001';
30 120 $VERSION = eval $VERSION;
31  
32 120 ▶$PDL::NiceSlice::debug = defined($PDL::NiceSlice::debug) ? $PDL::NiceSlice::debug : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $debug ? : 0 120 120
33 # replace all occurences of the form
34 #
35 # $pdl(args);
36 # or
37 # $pdl->(args);
38 # with
39 #
40 # $pdl->slice(processed_args);
41 #
42 #
43 # Modified 2-Oct-2001: don't modify $var(LIST) if it's part of a
44 # "for $var(LIST)" or "foreach $var(LIST)" statement. CED.
45 #
46 # Modified 5-Nov-2007: stop processing if we encounter m/^no\s+PDL\;:\;:NiceSlice\;\s*$/.
47  
48 # the next one is largely stolen from Regexp::Common
49 120 my $RE_cmt = qr'(?:(?:\#)(?:[^\n]*)(?:\n))';
50  
51 120 require PDL::Version; # get PDL version number
52 #
53 # remove code for PDL versions earlier than 2.3
54 #
55  
56 120 ▶use Text::Balanced; # used to find parenthesis-delimited blocks
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # Try overriding the current extract_quotelike() routine
59 # needed before using Filter::Simple to work around a bug
60 # between Text::Balanced and Filter::Simple for our purpose.
61 #
62  
63 BEGIN {
64  
65 120 ▶ no warnings; # quiet warnings for this
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
66  
67 sub Text::Balanced::extract_quotelike (;$$)
68 {
69 2 ▶ my $textref = $_[0] ? \$_[0] : \$_;
Subroutine extract_quotelike: called CC=4 SCAR=14.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 2 0 2
70 2 my $wantarray = wantarray;
71 2 ▶ my $pre = defined $_[1] ? $_[1] : '\s*';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 2 2
72
73 2 my @match = Text::Balanced::_match_quotelike($textref,$pre,0,0); # do not match // alone as m//
74 2 ▶ return Text::Balanced::_fail($wantarray, $textref) unless @match;
Branch: 1 branch
BranchTrueFalseTotal
unless @match 0 2 2
75 2 return Text::Balanced::_succeed($wantarray, $textref,
76 $match[2], $match[18]-$match[2], # MATCH
77 @match[18,19], # REMAINDER
78 @match[0,1], # PREFIX
79 @match[2..17], # THE BITS
80 @match[20,21], # ANY FILLET?
81 );
82 };
83  
84 };
85  
86  
87 # a call stack for error processing
88 120 my @callstack = ('stackbottom');
89 sub curarg {
90 2 ▶ my $arg = $callstack[-1]; # return top element of stack
Subroutine curarg: called CC=1 SCAR=0.0
Pod undocumented
91 2 $arg =~ s/\((.*)\)/$1/s;
92 2 return $arg;
93 }
94 4505 ▶sub savearg ($) {push @callstack,$_[0]}
Subroutine savearg: called CC=1 SCAR=0.0
Pod undocumented
95 1029 ▶sub poparg () {pop @callstack}
Subroutine poparg: called CC=1 SCAR=0.0
Pod undocumented
96  
97 120 my @srcstr = (); # stack for refs to current source strings
98 120 my $offset = 1; # line offset
99 120 my $file = 'unknown';
100  
101 120 my $mypostfix = '';
102  
103 sub autosever {
104 0 ▶ my ($this,$arg) = @_;
Subroutine autosever: not called CC=3 SCAR=24.8
Pod undocumented
105 0 ▶ $arg = 1 unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 0 0
106 0 ▶ if ($arg) {$mypostfix = '->sever'} else
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) { } 0 0 0
107 0 {$mypostfix = ''}
108 }
109  
110 sub line {
111 2 ▶ die __PACKAGE__." internal error: can't determine line number"
Subroutine line: called CC=2 SCAR=7.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $#srcstr < 0 0 2 2
112 if $#srcstr < 0;
113 2 my $pretext = substr ${$srcstr[0]}, 0, pos(${$srcstr[0]})-1;
114 2 return ($pretext =~ tr/\n/\n/)+$offset;
115 }
116  
117 sub filterdie {
118 2 ▶ my ($msg) = @_;
Subroutine filterdie: called CC=1 SCAR=0.0
Pod undocumented
119 2 die "$msg\n\t at $file near line ".
120 line().", slice expression '".curarg()."'\n";
121 }
122  
123 # non-bracketed prefix matching regexp
124 120 my $prebrackreg = qr/^([^\(\{\[]*)/;
125  
126 # split regex $re separated arglist
127 # but ignore bracket-protected bits
128 # (i.e. text that is within matched brackets)
129 sub splitprotected ($$) {
130 11436 ▶ my ($re,$txt) = @_;
Subroutine splitprotected: called CC=7 SCAR=19.5
Pod undocumented
131 11436 my ($got,$pre) = (1,'');
132 11436 my @chunks = ('');
133 11436 my $ct = 0; # infinite loop protection
134 11436 ▶ while ($got && $txt =~ /[({\[]/ && $ct++ < 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got and $txt =~ /[({\[]/ and $ct++ < 1000) 6050 11436 17486
Condition66%4/6
$got and $txt =~ /[({\[]/ and $ct++ < 1000
!ll&&!rl&&r
1143606050
$got and $txt =~ /[({\[]/
!ll&&!rl&&r
0114366050
MC/DC33%1/3
$got and $txt =~ /[({\[]/ and $ct++ < 1000
$got $txt =~ /[({\[]/ $ct++ < 1000
Truth table50%2/4
A and B and C
A= $gotB= $txt =~ /[({\[]/C= $ct++ < 1000
ABCresult
0XX0
10X0
1100
1111
135 # print "iteration $ct\n";
136 6050 ($got,$txt,$pre) =
137 Text::Balanced::extract_bracketed($txt,'{}()[]',$prebrackreg);
138 6050 my @partialargs = split $re, $pre, -1;
139 6050 ▶ $chunks[-1] .= shift @partialargs if @partialargs;
Branch: 1 branch
BranchTrueFalseTotal
if @partialargs 2180 3870 6050
140 6050 push @chunks, @partialargs;
141 6050 $chunks[-1] .= $got;
142 }
143 11436 ▶ filterdie "possible infinite parse loop, slice arg '".curarg()."'"
Branch: 1 branch
BranchTrueFalseTotal
if $ct == 1000 0 11436 11436
144 if $ct == 1000;
145 11436 my @partialargs = split $re, $txt, -1;
146 11436 ▶ $chunks[-1] .= shift @partialargs if @partialargs;
Branch: 1 branch
BranchTrueFalseTotal
if @partialargs 7796 3640 11436
147 11436 push @chunks, @partialargs;
148 11436 return @chunks;
149 }
150  
151 # a pattern that finds occurences of the form
152 #
153 # $var(
154 #
155 # and
156 #
157 # ->(
158 #
159 # used as the prefix pattern for findslice
160 120 my $prefixpat = qr/.*? # arbitrary leading stuff
161 ((?<!&)\$\w+ # $varname not preceded by '&'
162 |->) # or just '->'
163 (\s|$RE_cmt)* # ignore comments
164 \s* # more whitespace
165 (?=\()/smx; # directly followed by open '(' (look ahead)
166  
167 # translates a single arg into corresponding slice format
168 sub onearg ($) {
169 6919 ▶ my ($arg) = @_;
Subroutine onearg: called CC=15 SCAR=28.5
Pod undocumented
170 6919 ▶ print STDERR "processing arg '$arg'\n" if $PDL::NiceSlice::debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 6919 6919
171 6919 ▶ return q|'X'| if $arg =~ /^\s*:??\s*$/; # empty arg or just colon
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^\s*:??\s*$/ 246 6673 6919
172 # recursively process args for slice syntax
173 6673 ▶ $arg = findslice($arg,$PDL::debug) if $arg =~ $prefixpat;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /$prefixpat/msx 3 6670 6673
174 # no doubles colon are matched to avoid confusion with Perl's C<::>
175 6673 ▶ if ($arg =~ /(?<!:):(?!:)/) { # a start:stop:delta range
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /(?<!:):(?!:)/) 2428 4245 6673
176 2428 my @args = splitprotected '(?<!:):(?!:)', $arg;
177 2428 ▶ filterdie "invalid range in slice expression '".curarg()."'"
Branch: 1 branch
BranchTrueFalseTotal
if @args > 3 0 2428 2428
178 if @args > 3;
179 2428 ▶ $args[0] = 0 if !defined $args[0] || $args[0] =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args[0] or $args[0] =~ /^\s*$/ 0 2428 2428
Condition33%1/3
not defined $args[0] or $args[0] =~ /^\s*$/
l!l&&r!l&&!r
002428
MC/DC0%0/2
not defined $args[0] or $args[0] =~ /^\s*$/
not defined $args[0] $args[0] =~ /^\s*$/
Truth table33%1/3
not(A) or B
A= not defined $args[0]B= $args[0] =~ /^\s*$/
ABresult
1X1
011
000
180 2428 ▶ $args[1] = -1 if !defined $args[1] || $args[1] =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args[1] or $args[1] =~ /^\s*$/ 0 2428 2428
Condition33%1/3
not defined $args[1] or $args[1] =~ /^\s*$/
l!l&&r!l&&!r
002428
MC/DC0%0/2
not defined $args[1] or $args[1] =~ /^\s*$/
not defined $args[1] $args[1] =~ /^\s*$/
Truth table33%1/3
not(A) or B
A= not defined $args[1]B= $args[1] =~ /^\s*$/
ABresult
1X1
011
000
181 2428 ▶ $args[2] = undef if !defined $args[2] || $args[2] =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args[2] or $args[2] =~ /^\s*$/ 2427 1 2428
Condition66%2/3
not defined $args[2] or $args[2] =~ /^\s*$/
l!l&&r!l&&!r
242701
MC/DC50%1/2
not defined $args[2] or $args[2] =~ /^\s*$/
not defined $args[2] $args[2] =~ /^\s*$/
Truth table66%2/3
not(A) or B
A= not defined $args[2]B= $args[2] =~ /^\s*$/
ABresult
1X1
011
000
182 2428 return "[".join(',',@args)."]"; # replace single ':' with ','
183 }
184 # the (pos) syntax, i.e. 0D slice
185 4245 ▶ return "[$arg,0,0]" if $arg =~ s/^\s*\((.*)\)\s*$/$1/; # use the new [x,x,0]
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ s/^\s*\((.*)\)\s*$/$1/ 3018 1227 4245
186 # we don't allow [] syntax (although that's what slice uses)
187 1227 ▶ filterdie "invalid slice expression containing '[', expression was '".
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^\s*\[/ 0 1227 1227
188 curarg()."'" if $arg =~ /^\s*\[/;
189  
190 # If the arg starts with '*' it's a dummy call -- force stringification
191 # and prepend a '*' for handling by slice.
192 1227 ▶ return "(q(*).($arg))" if($arg =~ s/^\s*\*//);
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ s/^\s*\*// 240 987 1227
193  
194 # this must be a simple position, leave as is
195 987 return "$arg";
196 }
197  
198 # process the arg list
199 sub procargs {
200 4503 ▶ my ($txt) = @_;
Subroutine procargs: called CC=4 SCAR=14.5
Pod undocumented
201 4503 ▶ print STDERR "procargs: got '$txt'\n" if $PDL::NiceSlice::debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4503 4503
202 # $txt =~ s/^\s*\((.*)\)\s*$/$1/s; # this is now done by findslice
203 # push @callstack, $txt; # for later error reporting
204 my $args = $txt =~ /^\s*$/s ? '' :
205 4503 ▶ join ',', map {onearg $_} splitprotected ',', $txt;
Branch: 1 branch
BranchTrueFalseTotal
$txt =~ /^\s*$/s ? : 0 4503 4503
206 ## Leave whitespace/newlines in so line count
207 ## is preserved in error messages. Makes the
208 ## filtered output ugly---iffi the input was
209 ## ugly...
210 ##
211 ## $args =~ s/\s//sg; # get rid of whitespace
212 # pop @callstack; # remove from call stack
213 4503 ▶ print STDERR "procargs: returned '($args)'\n" if $PDL::NiceSlice::debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4503 4503
214 4503 return "($args)";
215 }
216  
217 # this is the real workhorse that translates occurences
218 # of $a(args) into $args->slice(processed_arglist)
219 #
220 sub findslice {
221 1031 ▶ my ($src,$verb) = @_;
Subroutine findslice: called CC=32 SCAR=35.9
Pod undocumented
222 1031 push @srcstr, \$src;
223 1031 ▶ $verb = 0 unless defined $verb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $verb 1028 3 1031
224 1031 my $processed = '';
225 1031 my $ct=0; # protect against infinite loop
226 1031 my ($found,$prefix,$dummy);
227 1031 ▶ while ( $src =~ m/\G($prefixpat)/ && (($found,$dummy,$prefix) =
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /\G($prefixpat)/ and (($found, $dummy, $prefix) = &Text::Balanced::extract_bracketed($src, "()", $prefixpat))[0] and $ct++ < 1000) 5253 1029 6282
Condition66%4/6
$src =~ /\G($prefixpat)/ and (($found, $dummy, $prefix) = &Text::Balanced::extract_bracketed($src, "()", $prefixpat))[0] and $ct++ < 1000
!ll&&!rl&&r
102905253
$src =~ /\G($prefixpat)/ and (($found, $dummy, $prefix) = &Text::Balanced::extract_bracketed($src, "()", $prefixpat))[0]
!ll&&!rl&&r
102905253
MC/DC33%1/3
$src =~ /\G($prefixpat)/ and (($found, $dummy, $prefix) = &Text::Balanced::extract_bracketed($src, "()", $prefixpat))[0] and $ct++ < 1000
$src =~ /\G($prefixpat)/ (($found, $dummy, $prefix) = &Text::Balanced::extract_bracketed($src, "()", $prefixpat))[0] $ct++ < 1000
Truth table50%2/4
A and B and C
A= $src =~ /\G($prefixpat)/B= (($found, $dummy, $prefix) = &Text::Balanced::extract_bracketed($src, "()", $prefixpat))[0]C= $ct++ < 1000
ABCresult
0XX0
10X0
1100
1111
228 Text::Balanced::extract_bracketed($src,'()',$prefixpat))[0]
229 && $ct++ < 1000) {
230 5253 ▶ print STDERR "pass $ct: found slice expr $found at line ".line()."\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verb 0 5253 5253
231 if $verb;
232  
233 # Do final check for "for $var(LIST)" and "foreach $var(LIST)" syntax.
234 # Process into an 'slice' call only if it's not that.
235  
236 5253 ▶ if ($prefix =~ m/for(each)?(\s+(my|our))?\s+\$\w+(\s|$RE_cmt)*$/s ||
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix =~ /for(each)?(\s+(my|our))?\s+\$\w+(\s|$RE_cmt)*$/s or $prefix =~ /->\s*\$\w+$/s) { } 748 4505 5253
Condition100%3/3
$prefix =~ /for(each)?(\s+(my|our))?\s+\$\w+(\s|$RE_cmt)*$/s or $prefix =~ /->\s*\$\w+$/s
l!l&&r!l&&!r
74714505
MC/DC100%2/2
$prefix =~ /for(each)?(\s+(my|our))?\s+\$\w+(\s|$RE_cmt)*$/s or $prefix =~ /->\s*\$\w+$/s
$prefix =~ /for(each)?(\s+(my|our))?\s+\$\w+(\s|$RE_cmt)*$/s $prefix =~ /->\s*\$\w+$/s
Truth table100%3/3
A or B
A= $prefix =~ /for(each)?(\s+(my|our))?\s+\$\w+(\s|$RE_cmt)*$/sB= $prefix =~ /->\s*\$\w+$/s
ABresult
1X1
011
000
237 # foreach statement: Don't translate
238 $prefix =~ m/->\s*\$\w+$/s) # e.g. $a->$method(args)
239 # method invocation via string, don't translate either
240 {
241 # note: even though we reject this one we need to call
242 # findslice on $found in case
243 # it contains slice expressions
244 748 $processed .= "$prefix".findslice($found);
245 } else { # statement is a real slice and not a foreach
246  
247 4505 my ($call,$pre,$post,$arg);
248  
249 # the following section got an overhaul in v0.99
250 # to fix modifier parsing and allow >1 modifier
251 # this code still needs polishing
252 4505 savearg $found; # error reporting
253 4505 ▶ print STDERR "findslice: found '$found'\n" if $PDL::NiceSlice::debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4505 4505
254 4505 $found =~ s/^\s*\((.*)\)\s*$/$1/s;
255 4505 my ($slicearg,@mods) = splitprotected ';', $found;
256 4505 ▶ filterdie "more than 1 modifier group: @mods" if @mods > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @mods > 1 0 4505 4505
257 # filterdie "invalid modifier $1"
258 # if $found =~ /(;\s*[[:graph:]]{2,}?\s*)\)$/;
259 4505 ▶ print STDERR "MODS: " . join(',',@mods) . "\n" if $PDL::NiceSlice::debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4505 4505
260 4505 my @post = (); # collects all post slice operations
261 4505 my @pre = ();
262 4505 ▶ if (@mods) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mods) { } 7 4498 4505
263 7 (my $mod = $mods[0]) =~ s/\s//sg; # eliminate whitespace
264 7 my @modflags = split '', $mod;
265 7 ▶ print STDERR "MODFLAGS: @modflags\n" if $PDL::NiceSlice::debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 7 7
266 7 ▶ filterdie "more than 1 modifier incompatible with ?: @modflags"
Branch: 1 branch
BranchTrueFalseTotal
if @modflags > 1 and grep /\?/, @modflags 1 6 7
Condition100%3/3
@modflags > 1 and grep /\?/, @modflags
!ll&&!rl&&r
331
MC/DC100%2/2
@modflags > 1 and grep /\?/, @modflags
@modflags > 1 grep /\?/, @modflags
Truth table100%3/3
A and B
A= @modflags > 1B= grep /\?/, @modflags
ABresult
0X0
100
111
267 if @modflags > 1 && grep (/\?/, @modflags); # only one flag with where
268 6 my %seen = ();
269 6 ▶ if (@modflags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@modflags) { } 5 1 6
270 5 for my $mod1 (@modflags) {
271 9 ▶ if ($mod1 eq '?') {
Branch: 4 branches
BranchTrueFalseTotal
if ($mod1 eq '?') { } 1 8 9
elsif ($mod1 eq '_') { } 1 7 8
elsif ($mod1 eq '|') { } 4 3 7
elsif ($mod1 eq '-') { } 3 0 3
272 1 ▶ $seen{$mod1}++ && filterdie "modifier $mod1 used twice or more";
Branch: 1 branch
BranchTrueFalseTotal
$seen{$mod1}++ and filterdie("modifier $mod1 used twice or more") 0 1 1
273 1 $call = 'where';
274 1 $arg = "(" . findslice($slicearg) . ")";
275 # $post = ''; # no post action required
276 } elsif ($mod1 eq '_') {
277 1 ▶ $seen{$mod1}++ && filterdie "modifier $mod1 used twice or more";
Branch: 1 branch
BranchTrueFalseTotal
$seen{$mod1}++ and filterdie("modifier $mod1 used twice or more") 0 1 1
278 1 push @pre, 'flat->';
279 1 ▶ $call ||= 'slice'; # do only once
Condition50%1/2
$call ||= "slice"
l!l
01
MC/DC0%0/1
$call ||= "slice"
$call
Truth table50%1/2
A
A= $call
Aresult
11
00
280 1 $arg = procargs($slicearg);
281 # $post = ''; # no post action required
282 } elsif ($mod1 eq '|') {
283 4 ▶ $seen{$mod1}++ && filterdie "modifier $mod1 used twice or more";
Branch: 1 branch
BranchTrueFalseTotal
$seen{$mod1}++ and filterdie("modifier $mod1 used twice or more") 1 3 4
284 3 ▶ $call ||= 'slice';
Condition50%1/2
$call ||= "slice"
l!l
30
MC/DC0%0/1
$call ||= "slice"
$call
Truth table50%1/2
A
A= $call
Aresult
11
00
285 3 ▶ $arg ||= procargs($slicearg);
Condition33%1/3
$arg ||= procargs($slicearg)
l!l&&r!l&&!r
300
MC/DC0%0/2
$arg ||= procargs($slicearg)
$arg procargs($slicearg)
Truth table33%1/3
A ||= B
A= $argB= procargs($slicearg)
ABresult
1X1
011
000
286 3 push @post, '->sever';
287 } elsif ($mod1 eq '-') {
288 3 ▶ $seen{$mod1}++ && filterdie "modifier $mod1 used twice or more";
Branch: 1 branch
BranchTrueFalseTotal
$seen{$mod1}++ and filterdie("modifier $mod1 used twice or more") 0 3 3
289 3 ▶ $call ||= 'slice';
Condition50%1/2
$call ||= "slice"
l!l
03
MC/DC0%0/1
$call ||= "slice"
$call
Truth table50%1/2
A
A= $call
Aresult
11
00
290 3 ▶ $arg ||= procargs($slicearg);
Condition33%1/3
$arg ||= procargs($slicearg)
l!l&&r!l&&!r
030
MC/DC0%0/2
$arg ||= procargs($slicearg)
$arg procargs($slicearg)
Truth table33%1/3
A ||= B
A= $argB= procargs($slicearg)
ABresult
1X1
011
000
291 3 push @post, '->reshape(-1)';
292 } else {
293 0 filterdie "unknown modifier $mod1";
294 }
295 }
296 } else { # empty modifier block
297 1 $call = 'slice';
298 1 $arg = procargs($slicearg);
299 # $post = '';
300 }
301 } else { # no modifier block
302 4498 $call = 'slice';
303 4498 $arg = procargs($slicearg);
304 # $post = '';
305 # $call = 'slice_if_pdl'; # handle runtime checks for $self type
306 # $arg =~ s/\)$/,q{$found})/; # add original argument string
307 # in case $self is not a piddle
308 # and the original call must be
309 # generated
310 }
311 4503 $pre = join '', @pre;
312 # assumption here: sever should be last
313 # and order of other modifiers doesn't matter
314 4503 $post = join '', sort @post; # need to ensure that sever is last
315 4503 ▶ $processed .= "$prefix". ($prefix =~ /->(\s*$RE_cmt*)*$/ ?
Branch: 1 branch
BranchTrueFalseTotal
$prefix =~ /->(\s*$RE_cmt*)*$/ ? : 3634 869 4503
316 '' : '->').
317 $pre.$call.$arg.$post.$mypostfix;
318 }
319  
320 } # end of while loop
321  
322 1029 poparg; # clean stack
323 1029 pop @srcstr; # clear stack
324 # append the remaining text portion
325 # use substr only if we have had at least one pass
326 # through above loop (otherwise pos is uninitialized)
327 1029 ▶ $processed .= $ct > 0 ? substr $src, pos($src) : $src;
Branch: 1 branch
BranchTrueFalseTotal
$ct > 0 ? : 153 876 1029
328 }
329  
330 ##############################
331 # termstr - generate a regexp to find turn-me-off strings
332 # CED 5-Nov-2007
333 sub terminator_regexp{
334 127 ▶ my $clstr = shift;
Subroutine terminator_regexp: called CC=1 SCAR=0.0
Pod undocumented
335 127 $clstr =~ s/([^a-zA-Z0-9])/\\$1/g;
336 127 my $termstr = '^\s*no\s+'.$clstr.'\s*;\s*(#.*)*$';
337 127 return qr/$termstr/o; # allow trailing comments
338 }
339  
340 sub reinstator_regexp{
341 1 ▶ my $clstr = shift;
Subroutine reinstator_regexp: called CC=1 SCAR=0.0
Pod undocumented
342 1 $clstr =~ s/([^a-zA-Z0-9])/\\$1/g;
343 1 my $reinstr = '^\s*use\s+'.$clstr.'\s*;\s*(#.*)*$';
344 1 return qr/$reinstr/o; # allow trailing comments
345 }
346  
347 # save eval of findslice that should be used within perldl or pdl2
348 # as a preprocessor
349 sub perldlpp {
350 1 ▶ my ($class, $txt) = @_;
Subroutine perldlpp: called CC=24 SCAR=48.2
Pod undocumented
351 1 local($_);
352 ##############################
353 # Backwards compatibility to before the two-parameter form. The only
354 # call should be around line 206 of PDL::AutoLoader, but one never
355 # knows....
356 # -- CED 5-Nov-2007
357 1 ▶ if(!defined($txt)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $txt) 0 1 1
358 0 print "PDL::NiceSlice::perldlpp -- got deprecated one-argument form, from ".(join("; ",caller))."...\n";
359 0 $txt = $class;
360 0 $class = "PDL::NiceSlice";
361 }
362  
363 ## Debugging to track exactly what is going on -- left in, in case it's needed again
364 1 ▶ if($PDL::debug > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::debug > 1) 0 1 1
365 0 print "PDL::NiceSlice::perldlpp - got:\n$txt\n";
366 0 my $i;
367 0 for $i(0..5){
368 0 my($package,$filename,$line,$subroutine, $hasargs) = caller($i);
369 0 printf("layer %d: %20s, %40s, line %5d, sub %20s, args: %d\n",$i,$package,$filename,$line,$subroutine,$hasargs);
370 }
371 }
372  
373 1 my $new;
374  
375 ##############################
376 ## This block sort-of echoes import(), below...
377 ## Crucial difference: we don't give up the ghost on termination conditions, only
378 ## mask out current findslices. That's because future uses won't be processed
379 ## (for some reason source filters don't work on evals).
380  
381 1 my @lines= split /\n/,$txt;
382  
383 1 my $terminator = terminator_regexp($class);
384 1 my $reinstator = reinstator_regexp($class);
385  
386 1 my($status, $off, $end);
387 1 eval {
388 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@lines and not $end and do { do { my $data = ""; while (@lines) { $_ = shift @lines; if (defined $terminator and /$terminator/) { $_ = "## $_"; $off = 1; last; } ; if (defined $reinstator and /$reinstator/) { $_ = "## $_"; } ; if (/^\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 and @lines) { $_ = shift @lines; if (defined $reinstator and /$reinstator/) { $off = 0; $_ = "## $_"; } ; if (defined $terminator and /$terminator/) { $_ = "## $_"; } ; $new .= "$_\n"; } } } 0 1 1
Condition33%1/3
@lines and not $end
!ll&&!rl&&r
100
MC/DC0%0/2
@lines and not $end
@lines not $end
Truth table33%1/3
A and not(B)
A= @linesB= not $end
ABresult
0X0
100
111
389 1 my $data = "";
390 1 ▶ while(@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 12 1 13
391 12 $_= shift @lines;
392 12 ▶ if(defined($terminator) && m/$terminator/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $terminator and /$terminator/) 0 12 12
Condition33%1/3
defined $terminator and /$terminator/
!ll&&!rl&&r
0120
MC/DC0%0/2
defined $terminator and /$terminator/
defined $terminator /$terminator/
Truth table33%1/3
A and B
A= defined $terminatorB= /$terminator/
ABresult
0X0
100
111
393 0 $_ = "## $_";
394 0 $off = 1;
395 0 last;
396 }
397 12 ▶ if(defined($reinstator) && m/$reinstator/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reinstator and /$reinstator/) 0 12 12
Condition33%1/3
defined $reinstator and /$reinstator/
!ll&&!rl&&r
0120
MC/DC0%0/2
defined $reinstator and /$reinstator/
defined $reinstator /$reinstator/
Truth table33%1/3
A and B
A= defined $reinstatorB= /$reinstator/
ABresult
0X0
100
111
398 0 $_ = "## $_";
399 }
400 12 ▶ if(m/^\s*(__END__|__DATA__)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(__END__|__DATA__)\s*$/) 0 12 12
401 0 $end=$1; $off = 1;
402 0 last;
403 }
404 12 $data .= "$_\n";
405 12 $count++;
406 12 $_="";
407 }
408 1 $_ = $data;
409 1 $_ = findslice $_ ;
410 1 ▶ $_ .= "no $class;\n" if $off;
Branch: 1 branch
BranchTrueFalseTotal
if $off 0 1 1
411 1 ▶ $_ .= "$end\n" if $end;
Branch: 1 branch
BranchTrueFalseTotal
if $end 0 1 1
412 1 $new .= "$_";
413
414 1 ▶ while($off && @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off and @lines) 0 1 1
Condition33%1/3
$off and @lines
!ll&&!rl&&r
100
MC/DC0%0/2
$off and @lines
$off @lines
Truth table33%1/3
A and B
A= $offB= @lines
ABresult
0X0
100
111
415 0 $_ = shift @lines;
416 0 ▶ if(defined($reinstator) && m/$reinstator/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reinstator and /$reinstator/) 0 0 0
Condition0%0/3
defined $reinstator and /$reinstator/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $reinstator and /$reinstator/
defined $reinstator /$reinstator/
Truth table0%0/3
A and B
A= defined $reinstatorB= /$reinstator/
ABresult
0X0
100
111
417 0 $off = 0;
418 0 $_ = "## $_";
419 }
420 0 ▶ if(defined($terminator) && m/$terminator/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $terminator and /$terminator/) 0 0 0
Condition0%0/3
defined $terminator and /$terminator/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $terminator and /$terminator/
defined $terminator /$terminator/
Truth table0%0/3
A and B
A= defined $terminatorB= /$terminator/
ABresult
0X0
100
111
421 0 $_ = "## $_";
422 }
423  
424 0 $new .= "$_\n";
425  
426 }
427 } while(@lines && !$end);
428 };
429
430 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
431 0 my $err = $@;
432 0 for (split '','#!|\'"%~/') {
433 0 ▶ return "print q${_}NiceSlice error: $err${_}"
Branch: 1 branch
BranchTrueFalseTotal
unless $err =~ /[$_]/ 0 0 0
434 unless $err =~ m{[$_]};
435 }
436 0 return "print q{NiceSlice error: $err}"; # if this doesn't work
437 # we're stuffed
438 }
439  
440 1 ▶ if($PDL::debug > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDL::debug > 1) 0 1 1
441 0 print "PDL::NiceSlice::perldlpp - returning:\n$new\n";
442 }
443 1 return $new;
444 }
445  
446 BEGIN {
447 120 ▶ require "$PDL::NiceSlice::engine";
Subroutine BEGIN: called CC=1 SCAR=0.0
448 }
449  
450 =head1 NAME
451  
452 PDL::NiceSlice - toward a nicer slicing syntax for PDL
453  
454 =head1 SYNOPSYS
455  
456 use PDL::NiceSlice;
457  
458 $a(1:4) .= 2; # concise syntax for ranges
459 print $b((0),1:$end); # use variables in the slice expression
460 $a->xchg(0,1)->(($pos-1)) .= 0; # default method syntax
461  
462 $idx = long 1, 7, 3, 0; # a piddle of indices
463 $a(-3:2:2,$idx) += 3; # mix explicit indexing and ranges
464 $a->clump(1,2)->(0:30); # 'default method' syntax
465 $a(myfunc(0,$var),1:4)++; # when using functions in slice expressions
466 # use parentheses around args!
467  
468 $b = $a(*3); # Add dummy dimension of order 3
469  
470 # modifiers are specified in a ;-separated trailing block
471 $a($a!=3;?)++; # short for $a->where($a!=3)++
472 $a(0:1114;_) .= 0; # short for $a->flat->(0:1114)
473 $b = $a(0:-1:3;|); # short for $a(0:-1:3)->sever
474 $n = sequence 3,1,4,1;
475 $b = $n(;-); # drop all dimensions of size 1 (AKA squeeze)
476 $b = $n(0,0;-|); # squeeze *and* sever
477 $c = $a(0,3,0;-); # more compact way of saying $a((0),(3),(0))
478  
479 =head1 DESCRIPTION
480  
481 Slicing is a basic, extremely common operation, and PDL's
482 L<slice|PDL::Slices/slice> method would be cumbersome to use in many
483 cases. C<PDL::NiceSlice> rectifies that by incorporating new slicing
484 syntax directly into the language via a perl I<source filter> (see
485 L<the perlfilter man page|perlfilter>). NiceSlice adds no new functionality, only convenient syntax.
486  
487 NiceSlice is loaded automatically in the perldl or pdl2 shell, but (to avoid
488 conflicts with other modules) must be loaded explicitly in standalone
489 perl/PDL scripts (see below). If you prefer not to use a prefilter on
490 your standalone scripts, you can use the L<slice|PDL::Slices/slice>
491 method in those scripts,
492 rather than the more compact NiceSlice constructs.
493  
494 =head1 Use in scripts and C<perldl> or C<pdl2> shell
495  
496 The new slicing syntax can be switched on and off in scripts
497 and perl modules by using or unloading C<PDL::NiceSlice>.
498  
499 But now back to scripts and modules.
500 Everything after C<use PDL::NiceSlice> will be translated
501 and you can use the new slicing syntax. Source filtering
502 will continue until the end of the file is encountered.
503 You can stop sourcefiltering before the end of the file
504 by issuing a C<no PDL::NiceSlice> statement.
505  
506 Here is an example:
507  
508 use PDL::NiceSlice;
509  
510 # this code will be translated
511 # and you can use the new slicing syntax
512  
513 no PDL::NiceSlice;
514  
515 # this code won't
516 # and the new slicing syntax will raise errors!
517  
518 See also L<Filter::Simple> and F<example> in this distribution for
519 further examples.
520  
521 NOTE: Unlike "normal" modules you need to include a
522 C<use PDL::NiceSlice> call in each and every file that
523 contains code that uses the new slicing syntax. Imagine
524 the following situation: a file F<test0.pl>
525  
526 # start test0.pl
527 use PDL;
528 use PDL::NiceSlice;
529  
530 $a = sequence 10;
531 print $a(0:4),"\n";
532  
533 require 'test1.pl';
534 # end test0.pl
535  
536 that C<require>s a second file F<test1.pl>
537  
538 # begin test1.pl
539 $aa = sequence 11;
540 print $aa(0:7),"\n";
541 1;
542 # end test1.pl
543  
544 Following conventional perl wisdom everything should be alright
545 since we C<use>d C<PDL> and C<PDL::NiceSlice> already from within
546 F<test0.pl> and by the time F<test1.pl> is C<require>d things should
547 be defined and imported, etc. A quick test run will, however, produce
548 something like the following:
549  
550 perl test0.pl
551 [0 1 2 3 4]
552 syntax error at test1.pl line 3, near "0:"
553 Compilation failed in require at test0.pl line 7.
554  
555 This can be fixed by adding the line
556  
557 use PDL::NiceSlice;
558  
559 C<before> the code in F<test1.pl> that uses the
560 new slicing syntax (to play safe just include the line
561 near the top of the file), e.g.
562  
563 # begin corrected test1.pl
564 use PDL::NiceSlice;
565 $aa = sequence 11;
566 print $aa(0:7),"\n";
567 1;
568 # end test1.pl
569  
570 Now things proceed more smoothly
571  
572 perl test0.pl
573 [0 1 2 3 4]
574 [0 1 2 3 4 5 6 7]
575  
576 Note that we don't need to issue C<use PDL> again.
577 C<PDL::NiceSlice> is a somewhat I<funny> module in
578 that respect. It is a consequence of the way source
579 filtering works in Perl (see also the IMPLEMENTATION
580 section below).
581  
582 =head2 evals and C<PDL::NiceSlice>
583  
584 Due to C<PDL::NiceSlice> being a source filter it won't work
585 in the usual way within evals. The following will I<not> do what
586 you want:
587  
588 $a = sequence 10;
589 eval << 'EOE';
590  
591 use PDL::NiceSlice;
592 $b = $a(0:5);
593  
594 EOE
595 print $b;
596  
597 Instead say:
598  
599 use PDL::NiceSlice;
600 $a = sequence 10;
601 eval << 'EOE';
602  
603 $b = $a(0:5);
604  
605 EOE
606 print $b;
607  
608 Source filters I<must> be executed at compile time to be effective. And
609 C<PDL::NiceFilter> is just a source filter (although it is not
610 necessarily obvious for the casual user).
611  
612 =head1 The new slicing syntax
613  
614 Using C<PDL::NiceSlice> slicing piddles becomes so much easier since, first of
615 all, you don't need to make explicit method calls. No
616  
617 $pdl->slice(....);
618  
619 calls, etc. Instead, C<PDL::NiceSlice> introduces two ways in which to
620 slice piddles without too much typing:
621  
622 =over 2
623  
624 =item *
625  
626 using parentheses directly following a scalar variable name,
627 for example
628  
629 $c = $b(0:-3:4,(0));
630  
631 =item *
632  
633 using the so called I<default method> invocation in which the
634 piddle object is treated as if it were a reference to a
635 subroutine (see also L<perlref>). Take this example that slices
636 a piddle that is part of a perl list C<@b>:
637  
638 $c = $b[0]->(0:-3:4,(0));
639  
640 =back
641  
642 The format of the argument list is the same for both types of
643 invocation and will be explained in more detail below.
644  
645 =head2 Parentheses following a scalar variable name
646  
647 An arglist in parentheses following directly after a scalar variable
648 name that is I<not> preceded by C<&> will be resolved as a slicing
649 command, e.g.
650  
651 $a(1:4) .= 2; # only use this syntax on piddles
652 $sum += $a(,(1));
653  
654 However, if the variable name is immediately preceded by a C<&>,
655 for example
656  
657 &$a(4,5);
658  
659 it will not be interpreted as a slicing expression. Rather, to avoid
660 interfering with the current subref syntax, it will be treated as an
661 invocation of the code reference C<$a> with argumentlist C<(4,5)>.
662  
663 The $a(ARGS) syntax collides in a minor way with the perl syntax. In
664 particular, ``foreach $var(LIST)'' appears like a PDL slicing call.
665 NiceSlice avoids translating the ``for $var(LIST)'' and
666 ``foreach $var(LIST)'' constructs for this reason. Since you
667 can't use just any old lvalue expression in the 'foreach' 'for'
668 constructs -- only a real perl scalar will do -- there's no
669 functionality lost. If later versions of perl accept
670 ``foreach <lvalue-expr> (LIST)'', then you can use the code ref
671 syntax, below, to get what you want.
672  
673 =head2 The I<default method> syntax
674  
675 The second syntax that will be recognized is what I called the
676 I<default method> syntax. It is the method arrow C<-E<gt>> directly
677 followed by an open parenthesis, e.g.
678  
679 $a->xchg(0,1)->(($pos)) .= 0;
680  
681 Note that this conflicts with the use of normal code references, since you
682 can write in plain Perl
683  
684 $sub = sub { print join ',', @_ };
685 $sub->(1,'a');
686  
687 NOTE: Once C<use PDL::NiceSlice> is in effect (you can always switch it off with
688 a line C<no PDL::NiceSlice;> anywhere in the script) the source filter will incorrectly
689 replace the above call to C<$sub> with an invocation of the slicing method.
690 This is one of the pitfalls of using a source filter that doesn't know
691 anything about the runtime type of a variable (cf. the
692 Implementation section).
693  
694 This shouldn't be a major problem in practice; a simple workaround is to use
695 the C<&>-way of calling subrefs, e.g.:
696  
697 $sub = sub { print join ',', @_ };
698 &$sub(1,'a');
699  
700 =head2 When to use which syntax?
701  
702 Why are there two different ways to invoke slicing?
703 The first syntax C<$a(args)> doesn't work with chained method calls. E.g.
704  
705 $a->xchg(0,1)(0);
706  
707 won't work. It can I<only> be used directly following a valid perl variable
708 name. Instead, use the I<default method> syntax in such cases:
709  
710 $a->xchg(0,1)->(0);
711  
712 Similarly, if you have a list of piddles C<@pdls>:
713  
714 $b = $pdls[5]->(0:-1);
715  
716 =head2 The argument list
717  
718 The argument list is a comma separated list. Each argument specifies
719 how the corresponding dimension in the piddle is sliced. In contrast
720 to usage of the L<slice|PDL::Slices/slice> method the arguments should
721 I<not> be quoted. Rather freely mix literals (1,3,etc), perl
722 variables and function invocations, e.g.
723  
724 $a($pos-1:$end,myfunc(1,3)) .= 5;
725  
726 There can even be other slicing commands in the arglist:
727  
728 $a(0:-1:$pdl($step)) *= 2;
729  
730 NOTE: If you use function calls in the arglist make sure that
731 you use parentheses around their argument lists. Otherwise the
732 source filter will get confused since it splits the argument
733 list on commas that are not protected by parentheses. Take
734 the following example:
735  
736 sub myfunc { return 5*$_[0]+$_[1] }
737 $a = sequence 10;
738 $sl = $a(0:myfunc 1, 2);
739 print $sl;
740 PDL barfed: Error in slice:Too many dims in slice
741 Caught at file /usr/local/bin/perldl, line 232, pkg main
742  
743  
744 The simple fix is
745  
746 $sl = $a(0:myfunc(1, 2));
747 print $sl;
748 [0 1 2 3 4 5 6 7]
749  
750 Note that using prototypes in the definition of myfunc does not help.
751 At this stage the source filter is simply not intelligent enough to
752 make use of this information. So beware of this subtlety.
753  
754 Another pitfall to be aware of: currently, you can't use the conditional
755 operator in slice expressions (i.e., C<?:>, since the parser confuses them
756 with ranges). For example, the following will cause an error:
757  
758 $a = sequence 10;
759 $b = rand > 0.5 ? 0 : 1; # this one is ok
760 print $a($b ? 1 : 2); # error !
761 syntax error at (eval 59) line 3, near "1,
762  
763 For the moment, just try to stay clear of the conditional operator
764 in slice expressions (or provide us with a patch to the parser to
765 resolve this issue ;).
766  
767 =head2 Modifiers
768  
769 Following a suggestion originally put forward by Karl Glazebrook the
770 latest versions of C<PDL::NiceSlice> implement I<modifiers> in slice
771 expressions. Modifiers are convenient shorthands for common variations
772 on PDL slicing. The general syntax is
773  
774 $pdl(<slice>;<modifier>)
775  
776 Four modifiers are currently implemented:
777  
778 =over
779  
780 =item *
781  
782 C<_> : I<flatten> the piddle before applying the slice expression. Here
783 is an example
784  
785 $b = sequence 3, 3;
786 print $b(0:-2;_); # same as $b->flat->(0:-2)
787 [0 1 2 3 4 5 6 7]
788  
789 which is quite different from the same slice expression without the modifier
790  
791 print $b(0:-2);
792 [
793 [0 1]
794 [3 4]
795 [6 7]
796 ]
797  
798 =item *
799  
800 C<|> : L<sever|PDL::Core/sever> the link to the piddle, e.g.
801  
802 $a = sequence 10;
803 $b = $a(0:2;|)++; # same as $a(0:2)->sever++
804 print $b;
805 [1 2 3]
806 print $a; # check if $a has been modified
807 [0 1 2 3 4 5 6 7 8 9]
808  
809 =item *
810  
811 C<?> : short hand to indicate that this is really a
812 L<where|PDL::Primitive/where> expression
813  
814 As expressions like
815  
816 $a->where($a>5)
817  
818 are used very often you can write that shorter as
819  
820 $a($a>5;?)
821  
822 With the C<?>-modifier the expression preceding the modifier is I<not>
823 really a slice expression (e.g. ranges are not allowed) but rather an
824 expression as required by the L<where|PDL::Primitive/where> method.
825 For example, the following code will raise an error:
826  
827 $a = sequence 10;
828 print $a(0:3;?);
829 syntax error at (eval 70) line 3, near "0:"
830  
831 That's about all there is to know about this one.
832  
833 =item *
834  
835 C<-> : I<squeeze> out any singleton dimensions. In less technical terms:
836 reduce the number of dimensions (potentially) by deleting all
837 dims of size 1. It is equivalent to doing a L<reshape|PDL::Core/reshape>(-1).
838 That can be very handy if you want to simplify
839 the results of slicing operations:
840  
841 $a = ones 3, 4, 5;
842 $b = $a(1,0;-); # easier to type than $a((1),(0))
843 print $b->info;
844 PDL: Double D [5]
845  
846 It also provides a unique opportunity to have smileys in your code!
847 Yes, PDL gives new meaning to smileys.
848  
849 =back
850  
851 =head2 Combining modifiers
852  
853 Several modifiers can be used in the same expression, e.g.
854  
855 $c = $a(0;-|); # squeeze and sever
856  
857 Other combinations are just as useful, e.g. C<;_|> to flatten and
858 sever. The sequence in which modifiers are specified is not important.
859  
860 A notable exception is the C<where> modifier (C<?>) which must not
861 be combined with other flags (let me know if you see a good reason
862 to relax this rule).
863  
864 Repeating any modifier will raise an error:
865  
866 $c = $a(-1:1;|-|); # will cause error
867 NiceSlice error: modifier | used twice or more
868  
869 Modifiers are still a new and experimental feature of
870 C<PDL::NiceSlice>. I am not sure how many of you are actively using
871 them. I<Please do so and experiment with the syntax>. I think
872 modifiers are very useful and make life a lot easier. Feedback is
873 welcome as usual. The modifier syntax will likely be further tuned in
874 the future but we will attempt to ensure backwards compatibility
875 whenever possible.
876  
877 =head2 Argument formats
878  
879 In slice expressions you can use ranges and secondly,
880 piddles as 1D index lists (although compare the description
881 of the C<?>-modifier above for an exception).
882  
883 =over 2
884  
885 =item * ranges
886  
887 You can access ranges using the usual C<:> separated format:
888  
889 $a($start:$stop:$step) *= 4;
890  
891 Note that you can omit the trailing step which then defaults to 1. Double
892 colons (C<::>) are not allowed to avoid clashes with Perl's namespace
893 syntax. So if you want to use steps different from the default
894 you have to also at least specify the stop position.
895 Examples:
896  
897 $a(::2); # this won't work (in the way you probably intended)
898 $a(:-1:2); # this will select every 2nd element in the 1st dim
899  
900 Just as with L<slice|PDL::Slices/slice> negative indices count from the end of the dimension
901 backwards with C<-1> being the last element. If the start index is larger
902 than the stop index the resulting piddle will have the elements in reverse
903 order between these limits:
904  
905 print $a(-2:0:2);
906 [8 6 4 2 0]
907  
908 A single index just selects the given index in the slice
909  
910 print $a(5);
911 [5]
912  
913 Note, however, that the corresponding dimension is not removed from
914 the resulting piddle but rather reduced to size 1:
915  
916 print $a(5)->info
917 PDL: Double D [1]
918  
919 If you want to get completely rid of that dimension enclose the index
920 in parentheses (again similar to the L<slice|PDL::Slices/slice> syntax):
921  
922 print $a((5));
923 5
924  
925 In this particular example a 0D piddle results. Note that this syntax is
926 only allowed with a single index. All these will be errors:
927  
928 print $a((0,4)); # will work but not in the intended way
929 print $a((0:4)); # compile time error
930  
931 An empty argument selects the whole dimension, in this example
932 all of the first dimension:
933  
934 print $a(,(0));
935  
936 Alternative ways to select a whole dimension are
937  
938 $a = sequence 5, 5;
939 print $a(:,(0));
940 print $a(0:-1,(0));
941 print $a(:-1,(0));
942 print $a(0:,(0));
943  
944 Arguments for trailing dimensions can be omitted. In that case
945 these dimensions will be fully kept in the sliced piddle:
946  
947 $a = random 3,4,5;
948 print $a->info;
949 PDL: Double D [3,4,5]
950 print $a((0))->info;
951 PDL: Double D [4,5]
952 print $a((0),:,:)->info; # a more explicit way
953 PDL: Double D [4,5]
954 print $a((0),,)->info; # similar
955 PDL: Double D [4,5]
956  
957 =item * dummy dimensions
958  
959 As in L<slice|slice>, you can insert a dummy dimension by preceding a
960 single index argument with '*'. A lone '*' inserts a dummy dimension of
961 order 1; a '*' followed by a number inserts a dummy dimension of that order.
962  
963 =item * piddle index lists
964  
965 The second way to select indices from a dimension is via 1D piddles
966 of indices. A simple example:
967  
968 $a = random 10;
969 $idx = long 3,4,7,0;
970 $b = $a($idx);
971  
972 This way of selecting indices was previously only possible using
973 L<dice|PDL::Slices/dice> (C<PDL::NiceSlice> attempts to unify the
974 C<slice> and C<dice> interfaces). Note that the indexing piddles must
975 be 1D or 0D. Higher dimensional piddles as indices will raise an error:
976  
977 $a = sequence 5, 5;
978 $idx2 = ones 2,2;
979 $sum = $a($idx2)->sum;
980 piddle must be <= 1D at /home/XXXX/.perldlrc line 93
981  
982 Note that using index piddles is not as efficient as using ranges.
983 If you can represent the indices you want to select using a range
984 use that rather than an equivalent index piddle. In particular,
985 memory requirements are increased with index piddles (and execution
986 time I<may> be longer). That said, if an index piddle is the way to
987 go use it!
988  
989 =back
990  
991 As you might have expected ranges and index piddles can be freely
992 mixed in slicing expressions:
993  
994 $a = random 5, 5;
995 $b = $a(-1:2,pdl(3,0,1));
996  
997 =head2 piddles as indices in ranges
998  
999 You can use piddles to specify indices in ranges. No need to
1000 turn them into proper perl scalars with the new slicing syntax.
1001 However, make sure they contain not more than one element! Otherwise
1002 a runtime error will be triggered. First a couple of examples that
1003 illustrate proper usage:
1004  
1005 $a = sequence 5, 5;
1006 $rg = pdl(1,-1,3);
1007 print $a($rg(0):$rg(1):$rg(2),2);
1008 [
1009 [11 14]
1010 ]
1011 print $a($rg+1,:$rg(0));
1012 [
1013 [2 0 4]
1014 [7 5 9]
1015 ]
1016  
1017 The next one raises an error
1018  
1019 print $a($rg+1,:$rg(0:1));
1020 multielement piddle where only one allowed at XXX/Core.pm line 1170.
1021  
1022 The problem is caused by using the 2-element piddle C<$rg(0:1)> as the
1023 stop index in the second argument C<:$rg(0:1)> that is interpreted as
1024 a range by C<PDL::NiceSlice>. You I<can> use multielement piddles as
1025 index piddles as described above but not in ranges. And
1026 C<PDL::NiceSlice> treats any expression with unprotected C<:>'s as a
1027 range. I<Unprotected> means as usual
1028 I<"not occurring between matched parentheses">.
1029  
1030 =head1 IMPLEMENTATION
1031  
1032 C<PDL::NiceSlice> exploits the ability of Perl to use source filtering
1033 (see also L<perlfilter>). A source filter basically filters (or
1034 rewrites) your perl code before it is seen by the
1035 compiler. C<PDL::NiceSlice> searches through your Perl source code and when
1036 it finds the new slicing syntax it rewrites the argument list
1037 appropriately and splices a call to the C<slice> method using the
1038 modified arg list into your perl code. You can see how this works in
1039 the L<perldl|perldl> or L<pdl2|PDL::Perldl2> shells by switching on
1040 reporting (see above how to do that).
1041  
1042 =head1 BUGS
1043  
1044 =head2 Conditional operator
1045  
1046 The conditional operator can't be used in slice expressions (see
1047 above).
1048  
1049 =head2 The C<DATA> file handle
1050  
1051 I<Note>: To avoid clobbering the C<DATA> filehandle C<PDL::NiceSlice>
1052 switches itself off when encountering the C<__END__> or C<__DATA__> tokens.
1053 This should not be a problem for you unless you use C<SelfLoader> to load
1054 PDL code including the new slicing from that section. It is even desirable
1055 when working with L<Inline::Pdlpp|Inline::Pdlpp>, see below.
1056  
1057 =head2 Possible interaction with L<Inline::Pdlpp|Inline::Pdlpp>
1058  
1059 There is currently an undesired interaction between C<PDL::NiceSlice>
1060 and the new L<Inline::Pdlpp|Inline::Pdlpp> module (currently only in
1061 PDL CVS). Since PP code generally
1062 contains expressions of the type C<$var()> (to access piddles, etc)
1063 C<PDL::NiceSlice> recognizes those I<incorrectly> as
1064 slice expressions and does its substitutions. This is not a problem
1065 if you use the C<DATA> section for your Pdlpp code -- the recommended
1066 place for Inline code anyway. In that case
1067 C<PDL::NiceSlice> will have switched itself off before encountering any
1068 Pdlpp code (see above):
1069  
1070 # use with Inline modules
1071 use PDL;
1072 use PDL::NiceSlice;
1073 use Inline Pdlpp;
1074  
1075 $a = sequence(10);
1076 print $a(0:5);
1077  
1078 __END__
1079  
1080 __Pdlpp__
1081  
1082 ... inline stuff
1083  
1084  
1085 Otherwise switch C<PDL::NiceSlice> explicitly off around the
1086 Inline::Pdlpp code:
1087  
1088 use PDL::NiceSlice;
1089  
1090 $a = sequence 10;
1091 $a(0:3)++;
1092 $a->inc;
1093  
1094 no PDL::NiceSlice; # switch off before Pdlpp code
1095 use Inline Pdlpp => "Pdlpp source code";
1096  
1097 The cleaner solution is to always stick with the
1098 C<DATA> way of including your C<Inline> code as
1099 in the first example. That way you keep your nice Perl
1100 code at the top and all the ugly Pdlpp stuff etc at
1101 the bottom.
1102  
1103 =head2 Bug reports
1104  
1105 Feedback and bug reports are welcome. Please include an example
1106 that demonstrates the problem. Log bug reports in the PDL
1107 issues tracker at L<https://github.com/PDLPorters/pdl/issues>
1108 or send them to the pdl-devel mailing list
1109 (see L<http://pdl.perl.org/?page=mailing-lists>).
1110  
1111  
1112 =head1 COPYRIGHT
1113  
1114 Copyright (c) 2001, 2002 Christian Soeller. All Rights Reserved.
1115 This module is free software. It may be used, redistributed
1116 and/or modified under the same terms as PDL itself
1117 (see L<http://pdl.perl.org>).
1118  
1119 =cut
1120  
1121 120 1;
« blib/lib/PDL/IO/Pnm.pm ↑ Summary blib/lib/PDL/Primitive.pm »