blib/lib/PGPLOT/Device.pm

filter: Statementstmt 15.6% 18 / 115 Branchbran 1.5% 1 / 66 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 25.0% 5 / 20 Podpod 100% 11 / 11 totaltotal 15.4% 35 / 226 CC 40 SCAR 45.8
CC
40
Cov
10%
_parse_spec
240.0
_initialize
132.0
_stringify
69.9
CRAP 98.0
↑ Summary blib/lib/PGPLOT/Device/PGWin.pm »
1 package PGPLOT::Device;
2  
3 # ABSTRACT: autogenerate PGPLOT device names
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.13';
9  
10  
11 3 my %Default = ( device => 'xs', );
12  
13 3 my $NDevices;
14 my %PGDevice;
15 3 my %DevMap;
16  
17 sub _croak {
18 0 ▶ require Carp;
Subroutine _croak: not called CC=1 SCAR=6.9
19 0 goto \&Carp::croak;
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 sub new {
112 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=3.1
Pod documented
113  
114 1 _class_init();
115  
116 0 my $self = {
117 devn => 1,
118 last => undef,
119 vars => {},
120 ask => undef,
121 };
122 0 bless $self, $class;
123  
124 0 $self->_initialize( @_ );
125  
126 # need to keep track of whether there was an initial prefix
127 0 $self->{init_prefix} = defined $self->{prefix};
128  
129 0 $self;
130 }
131  
132 sub _class_init {
133 1 ▶ return if $NDevices;
Subroutine _class_init: called CC=3 SCAR=20.0
Branch: 1 branch
BranchTrueFalseTotal
if $NDevices 0 1 1
134  
135 1 require PGPLOT;
136 0 PGPLOT::pgqndt( $NDevices );
137  
138 0 my @devices;
139  
140 0 for my $didx ( 1 .. $NDevices ) {
141 0 my ( $type, $tlen, $descr, $dlen, $inter );
142 0 PGPLOT::pgqdt( $didx, $type, $tlen, $descr, $dlen, $inter );
143 0 $type =~ s{/}{};
144 0 $PGDevice{ lc $type } = {
145 idx => $didx,
146 type => lc( $type ),
147 tlen => $tlen,
148 descr => $descr,
149 dlen => $dlen,
150 inter => $inter,
151 };
152  
153 0 push @devices, lc $type;
154 }
155  
156 0 require Text::Abbrev;
157 0 Text::Abbrev::abbrev( \%DevMap, @devices );
158 }
159  
160  
161 sub _initialize {
162 0 ▶ my $opts = 'HASH' eq ref $_[-1] ? pop @_ : {};
Subroutine _initialize: not called CC=11 SCAR=48.8
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq ref $_[-1] ? : 0 0 0
163  
164 0 my ( $self, $spec ) = @_;
165  
166 0 ▶ my %spec = defined $spec ? $self->_parse_spec( $spec ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $spec ? : 0 0 0
167  
168 # don't allow an override to change the device
169 0 ▶ delete $spec{device} if defined $self->{device};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"device"} 0 0 0
170  
171 # don't allow an override to change an initial prefix
172 0 ▶ delete $spec{prefix} if $self->{init_prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"init_prefix"} 0 0 0
173  
174 # fill the object
175 0 $self->{$_} = $spec{$_} for keys %spec;
176  
177 0 ▶ unless ( defined $self->{device} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"device"}) 0 0 0
178 0 $self->{device} = $Default{device};
179 0 $self->{devinfo} = $PGDevice{ $DevMap{ $Default{device} } };
180 }
181  
182 0 ▶ if ( exists $opts->{vars} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"vars"}) 0 0 0
183 _croak( "vars attribute must be a hash\n" )
184 0 ▶ unless 'HASH' eq ref $opts->{vars};
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $opts->{"vars"} 0 0 0
185  
186 0 $self->{vars} = $opts->{vars};
187 }
188  
189 0 ▶ $self->{ask} = $opts->{ask} // ( $self->is_interactive && $self->is_const );
Condition0%0/6
$opts->{"ask"} // $self->is_interactive && $self->is_const
l!l&&r!l&&!r
000
$self->is_interactive and $self->is_const
!ll&&!rl&&r
000
MC/DC0%0/3
$opts->{"ask"} // $self->is_interactive && $self->is_const
$opts->{"ask"} $self->is_interactive $self->is_const
Truth table0%0/4
A // B and C
A= $opts->{"ask"}B= $self->is_interactiveC= $self->is_const
ABCresult
1XX1
0111
00X0
0100
190  
191 0 $self;
192 }
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210 sub override {
211 0 ▶ my $self = shift;
Subroutine override: not called CC=2 SCAR=17.9
Pod documented
212  
213 0 ▶ if ( !$self->is_interactive() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_interactive) 0 0 0
214 0 $self->_initialize( @_ );
215 }
216  
217 0 $self;
218 }
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230 sub devn {
231 0 ▶ my $self = shift;
Subroutine devn: not called CC=2 SCAR=17.9
Pod documented
232 0 my $old = $self->{devn};
233 0 ▶ $self->{devn} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
234  
235 0 $old;
236 }
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249 0 ▶sub ask { $_[0]->{ask} }
Subroutine ask: not called CC=1 SCAR=6.9
Pod documented
250  
251  
252 sub _parse_spec {
253 0 ▶ my ( $self, $spec ) = @_;
Subroutine _parse_spec: not called CC=15 SCAR=54.8
254 0 my ( $prefix, $device );
255 0 my %spec;
256  
257  
258 # split into prefix and /device. set to prefix only, if no match,
259 # as that'll be the case if no device was specified.
260 0 ▶ $prefix = $spec
Branch: 1 branch
BranchTrueFalseTotal
if 0 == (() = $spec =~ m[(.*)/([^/]+)$]) 0 0 0
261 if 0 == ( ( $prefix, $device ) = $spec =~ m{(.*)/([^/]+)$} );
262  
263  
264 # be careful that a multi-directory path (dir/prefix) doesn't get
265 # translated into file/device. If /prefix looks like a real PGPLOT
266 # device, this will fail horribly.
267  
268 # if there's already a device, discard /device if it looks like
269 # a PGPLOT device, else append it to prefix.
270  
271 # if there's not already a device, /device had better look like
272 # a PGPLOT device.
273  
274 0 ▶ if ( defined $device && !exists $DevMap{ lc $device } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $device and not exists $DevMap{lc $device}) 0 0 0
Condition0%0/3
defined $device and not exists $DevMap{lc $device}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $device and not exists $DevMap{lc $device}
defined $device not exists $DevMap{lc $device}
Truth table0%0/3
A and not(B)
A= defined $deviceB= not exists $DevMap{lc $device}
ABresult
0X0
100
111
275 0 ▶ if ( defined $self->{device} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'device'}) { } 0 0 0
276 0 $prefix .= q{/} . $device;
277 0 undef $device;
278 }
279  
280 # no pre-existing device. make sure that the device is a real one
281 else {
282 0 _croak( "unknown PGPLOT device: $device\n" );
283 }
284 }
285  
286 # if device isn't defined, use the existing one for the object
287 0 ▶ $spec{device} = defined $device ? lc( $device ) : $self->{device};
Branch: 1 branch
BranchTrueFalseTotal
defined $device ? : 0 0 0
288 0 $spec{devinfo} = $PGDevice{ $DevMap{ $spec{device} } };
289 0 $spec{prefix} = $prefix;
290  
291 0 ▶ if ( $prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 0 0 0
292 # numeric (possibly autoincrement)
293 0 ▶ if ( $prefix =~ /^([+])?(\d+)?$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefix =~ /^([+])?(\d+)?$/) { } 0 0 0
elsif (defined $spec{'device'}) { } 0 0 0
294 0 ▶ $spec{devn} = defined $2 ? $2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 0 0 0
295  
296 # if +, autoincrement device number
297 # we use interpolation to handle this case
298 0 ▶ $spec{prefix} = defined $1 ? '${devn}' : $2;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 0 0
299 }
300  
301 elsif ( defined $spec{device} ) {
302 0 ▶ if ( !$spec{devinfo}{inter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $spec{'devinfo'}{'inter'}) { } 0 0 0
303 my $ext
304 = ( $spec{device} =~ m{^v?c?(ps)$}i )
305 ? ".$1"
306 0 ▶ : q{.} . $spec{device};
Branch: 1 branch
BranchTrueFalseTotal
$spec{'device'} =~ /^v?c?(ps)$/i ? : 0 0 0
307  
308 # make sure the appropriate suffix is in there
309 0 $prefix =~ s/${ext}$//;
310 0 $prefix .= $ext;
311 0 $spec{prefix} = $prefix;
312 }
313  
314 # we've got a situation here. an interactive device with a nonparseable
315 # prefix. better bail
316 else {
317 0 _croak( "error: interactive device with unparseable prefix: $spec\n" );
318 }
319 }
320 }
321  
322 # only defined keys get through. makes it easier to override
323 # things
324 delete $spec{$_}
325 0 ▶ for grep { !defined $spec{$_} || q{} eq $spec{$_} } keys %spec;
Branch: 1 branch
BranchTrueFalseTotal
not defined $spec{$_} or "" eq $spec{$_} 0 0 0
326  
327 0 %spec;
328 }
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339  
340  
341 sub next { ## no critic ( BuiltinHomonyms )
342 0 ▶ my $self = shift;
Subroutine next: not called CC=1 SCAR=6.9
Pod documented
343  
344 0 $self->{last} = $self->_stringify;
345 0 $self->{devn}++;
346 0 $self->{last};
347 }
348  
349  
350  
351  
352  
353  
354  
355  
356  
357  
358 sub current {
359 0 ▶ my $self = shift;
Subroutine current: not called CC=1 SCAR=6.9
Pod documented
360  
361 0 $self->_stringify;
362 }
363  
364  
365  
366  
367  
368  
369  
370  
371  
372  
373 sub last { ## no critic ( AmbiguousNames BuiltinHomonyms )
374 0 ▶ my $self = shift;
Subroutine last: not called CC=1 SCAR=6.9
Pod documented
375 0 $self->{last};
376 }
377  
378 sub _compare {
379 0 ▶ my ( $self, $other, $reverse ) = @_;
Subroutine _compare: not called CC=2 SCAR=17.9
380  
381 0 ▶ $reverse ? $other cmp $self->_stringify : $self->_stringify cmp $other;
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 0 0
382 }
383  
384  
385  
386  
387  
388  
389  
390  
391  
392  
393 sub is_const {
394 0 ▶ my $self = shift;
Subroutine is_const: not called CC=2 SCAR=17.9
Pod documented
395 defined $self->{prefix}
396 ? ( $self->_stringify eq $self->{prefix} . q{/} . $self->{device} )
397 0 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'prefix'} ? : 0 0 0
398 }
399  
400  
401  
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412 sub would_change {
413 0 ▶ my $self = shift;
Subroutine would_change: not called CC=2 SCAR=17.9
Pod documented
414  
415 0 ▶ return defined $self->{last} ? $self->_stringify ne $self->{last} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'last'} ? : 0 0 0
416 }
417  
418  
419  
420  
421  
422  
423  
424  
425  
426 sub is_interactive {
427 0 ▶ my $self = shift;
Subroutine is_interactive: not called CC=1 SCAR=6.9
Pod documented
428  
429 0 $self->{devinfo}{inter};
430 }
431  
432  
433  
434  
435  
436  
437  
438  
439  
440  
441 sub is_ephemeral {
442 0 ▶ my $self = shift;
Subroutine is_ephemeral: not called CC=1 SCAR=6.9
Pod documented
443 0 $self->{device} eq 'xw';
444 }
445  
446  
447 sub _stringify {
448 0 ▶ my $self = shift;
Subroutine _stringify: not called CC=9 SCAR=42.5
449  
450 # handle interpolated values
451 0 ▶ my $prefix = defined $self->{prefix} ? $self->{prefix} : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'prefix'} ? : 0 0 0
452  
453 # get calling package
454  
455 0 my ( $fmt, $val );
456  
457 ## no critic ( ProhibitNoStrict );
458 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
459 0 my $pkg = ( caller( 1 ) )[0];
460 0 ▶ 1 while $prefix =~ s/ [\$][{] (\w+) (?::([^}]+))? } /
Branch: 1 branch
BranchTrueFalseTotal
$prefix =~ s/ [\$][{] (\w+) (?::([^}]+))? } /$fmt = defined $2 ? $2 : "%s"; $val = $1 eq "devn" ? $self->{"devn"} : (exists $self->{"vars"}{$1} ? "SCALAR" eq ref $self->{"vars"}{$1} ? ${$self->{"vars"}{$1};} : $self->{"vars"}{$1} : (defined ${*{"${pkg}::$1";}{"SCALAR"};} ? ${*{"${pkg}::$1";}{"SCALAR"};} : undef)); sprintf $fmt, $val if defined $val;/ex and do { '???' } 0 0 0
461 0 ▶ $fmt = defined $2 ? $2 : '%s';
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 0 0 0
462  
463 $val =
464  
465 # special: device id
466 $1 eq 'devn' ? $self->{devn} :
467  
468 # part of the user passed set of variables?
469 exists $self->{vars}{$1} ?
470  
471 # dereference it if it's a scalar ref, else use it directly
472 ( 'SCALAR' eq ref $self->{vars}{$1} ?
473 0 ▶ ${$self->{vars}{$1}} : $self->{vars}{$1} ) :
Branch: 1 branch
BranchTrueFalseTotal
defined ${*{"${pkg}::$1";}{'SCALAR'};} ? : 0 0 0
474  
475 # is it in the parent package?
476 0 ▶ defined ${*{"${pkg}::$1"}{SCALAR}} ? ${*{"${pkg}::$1"}{SCALAR}} :
Branch: 3 branches
BranchTrueFalseTotal
$1 eq 'devn' ? : 0 0 0
exists $self->{'vars'}{$1} ? : 0 0 0
'SCALAR' eq ref $self->{'vars'}{$1} ? : 0 0 0
477  
478 # nothing
479 undef;
480  
481 0 ▶ sprintf( $fmt, $val ) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
482 /ex;
483  
484 0 $prefix . q{/} . $self->{device};
485 }
486  
487 3 1;
488  
489 #
490 # This file is part of PGPLOT-Device
491 #
492 # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
493 #
494 # This is free software, licensed under:
495 #
496 # The GNU General Public License, Version 3, June 2007
497 #
498  
499 __END__
↑ Summary blib/lib/PGPLOT/Device/PGWin.pm »