lib/Template/Plugin/Scalar.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 70.0% 7 / 10 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 11 / 11 Podpod 66.6% 2 / 3 totaltotal 93.8% 76 / 81 CC 7 SCAR 9.0
CC
7
Cov
94%
AUTOLOAD
4.2
AUTOLOAD
4.0
BEGIN
1.0
CRAP 2.5
« lib/Template/Plugin/Procedural.pm ↑ Summary lib/Template/Exception.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::Scalar
4 #
5 # DESCRIPTION
6 # Template Toolkit plugin module which allows you to call object methods
7 # in scalar context.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 2008-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #============================================================================
19  
20 package Template::Plugin::Scalar;
21 1 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 our $VERSION = '3.106';
28 1 our $MONAD = 'Template::Monad::Scalar';
29 1 our $EXCEPTION = 'Template::Exception';
30 1 our $AUTOLOAD;
31  
32 1 ▶sub load {
Subroutine load: called CC=1 SCAR=0.0
Pod documented
33 1 my $class = shift;
34 1 my $context = shift;
35  
36 # define .scalar vmethods for hash and list objects
37 1 $context->define_vmethod( hash => scalar => \&scalar_monad );
38 1 $context->define_vmethod( list => scalar => \&scalar_monad );
39  
40 1 return $class;
41 }
42  
43 4 ▶sub scalar_monad {
Subroutine scalar_monad: called CC=1 SCAR=0.0
Pod undocumented
44 # create a .scalar monad which wraps the hash- or list-based object
45 # and delegates any method calls back to it, calling them in scalar
46 # context, e.g. foo.scalar.bar becomes $MONAD->new($foo)->bar and
47 # the monad calls $foo->bar in scalar context
48 4 $MONAD->new(shift);
49 }
50  
51 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52 4 my ($class, $context, @args) = @_;
53 # create a scalar plugin object which will lookup a variable subroutine
54 # and call it. e.g. scalar.foo results in a call to foo() in scalar context
55 4 my $self = bless {
56 _CONTEXT => $context,
57 }, $class;
58 4 return $self;
59 }
60  
61 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=14.5
62 1 my $self = shift;
63 1 my $item = $AUTOLOAD;
64 1 $item =~ s/.*:://;
65 1 ▶ return if $item eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "DESTROY" 0 1 1
66  
67 # lookup the named values
68 1 my $stash = $self->{ _CONTEXT }->stash;
69 1 my $value = $stash->{ $item };
70  
71 1 ▶ if (! defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 1 1
elsif (ref $value eq 'CODE') { } 1 0 1
72 0 die $EXCEPTION->new( scalar => "undefined value for scalar call: $item" );
73 }
74 elsif (ref $value eq 'CODE') {
75 1 $value = $value->(@_);
76 }
77 1 return $value;
78 }
79  
80  
81 package Template::Monad::Scalar;
82  
83 1 our $EXCEPTION = 'Template::Exception';
84 1 our $AUTOLOAD;
85  
86 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
87 4 my ($class, $this) = @_;
88 4 bless \$this, $class;
89 }
90  
91 8 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
92 8 my $self = shift;
93 8 my $this = $$self;
94 8 my $item = $AUTOLOAD;
95 8 $item =~ s/.*:://;
96 8 ▶ return if $item eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "DESTROY" 4 4 8
97  
98 4 my $method;
99 4 ▶ if (Scalar::Util::blessed($this)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($this)) { } 3 1 4
100 # lookup the method...
101 3 ▶ $method = $this->can($item)
Condition100%2/2
$this->can($item) || die($Template::Monad::Scalar::EXCEPTION->new('scalar', "no such method: $item"))
l!l
21
MC/DC100%1/1
$this->can($item) || die($Template::Monad::Scalar::EXCEPTION->new('scalar', "no such method: $item"))
$this->can($item)
Truth table100%2/2
A
A= $this->can($item)
Aresult
11
00
102 || die $EXCEPTION->new( scalar => "no such method: $item" );
103 }
104 else {
105 1 die $EXCEPTION->new( scalar => "invalid object method: $item" );
106 }
107  
108 # ...and call it in scalar context
109 2 my $result = $method->($this, @_);
110  
111 2 return $result;
112 }
113  
114 1 1;
115  
116 __END__
« lib/Template/Plugin/Procedural.pm ↑ Summary lib/Template/Exception.pm »