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