File Coverage

blib/lib/Array/PseudoScalar.pm
Criterion Covered Total %
statement 39 41 95.1
branch 5 8 62.5
condition n/a
subroutine 11 12 91.6
pod 3 3 100.0
total 58 64 90.6


line stmt bran cond sub pod time code
1             package Array::PseudoScalar;
2              
3 3     3   81510 use 5.006;
  3         10  
  3         118  
4 3     3   17 use strict;
  3         7  
  3         102  
5 3     3   17 use warnings;
  3         9  
  3         348  
6              
7             our $VERSION = "1.02";
8              
9             # subclass constructor
10             sub subclass {
11 4     4 1 546 my ($class, $sep) = @_;
12 4 50       14 $sep or die $class . '->subclass(..): missing a nonempty separator string';
13              
14 4         14 my $subclass = join '::', $class, $sep;
15              
16             # deactivate strict refs because we'll be playing with symbol tables
17 3     3   23 no strict 'refs';
  3         6  
  3         722  
18              
19             # build the subclass on the fly, if not already present
20 4 100       5 @{$subclass.'::ISA'} = ($class) unless @{$subclass.'::ISA'};
  3         56  
  4         44  
21              
22 4         13 return $subclass;
23             }
24              
25             # instance constructor
26             sub new {
27 4     4 1 32 my $class = shift;
28 4 50       12 $class ne __PACKAGE__
29             or die "can't call ->new(..) on $class; call ->subclass(..) first";
30 4         23 bless [@_], $class;
31             }
32              
33             # stringification method
34             sub _stringify {
35 11     11   81114 my $self = shift;
36 11         22 my $class = ref $self;
37 11         42 my @class_path = split /::/, $class;
38 11 50       35 @class_path > 2 or die "$class: you forgot to call ->subclass(..)";
39 11         20 my $sep = $class_path[-1];
40 11         78 return join $sep, @$self;
41             }
42              
43             # overload API
44             use overload
45 3         22 '""' => \&_stringify, # stringification
46             fallback => 1, # all other operators will be derived from '""'
47 3     3   5350 ;
  3         4581  
48              
49             # be treated like an plain array by JSON::to_json()
50 0     0 1 0 sub TO_JSON {my $self = shift; return [@$self]}
  0         0  
51              
52             # additional methods for Template Toolkit, if present on this system
53 3     3   118744 if (eval "use Template; 1") {
  3         119758  
  3         56  
54             require Template::Stash;
55              
56             # deactivate strict refs because we'll be playing with symbol tables
57 3     3   467 no strict 'refs';
  3         7  
  3         404  
58              
59             # method for being treated as a list by Template::Iterator
60 1     1   26 *as_list = sub {my $self = shift; return $self};
  1         4  
61              
62             # import vmethods from Template::Stash, to be treated as a scalar
63             foreach my $meth_name (keys %$Template::Stash::SCALAR_OPS) {
64              
65             # won't shadow list vmethods
66             next if exists $Template::Stash::LIST_OPS->{$meth_name};
67              
68             *$meth_name = $Template::Stash::SCALAR_OPS->{$meth_name};
69             }
70             }
71              
72              
73             1; # End of Array::PseudoScalar
74              
75             __END__