MetaText/Directive.pm

filter: Statementstmt 76.0% 19 / 25 Branchbran 50.0% 5 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 3 / 5 Podpod 0.0% 0 / 2 totaltotal 64.2% 27 / 42 CC 7 SCAR 17.9
CC
7
Cov
69%
_error
12.0
new
5.1
error
2.0
CRAP 6.0
« MetaText/Factory.pm ↑ Summary
1 #============================================================================
2 #
3 # Text::MetaText::Directive
4 #
5 # DESCRIPTION
6 # A very simple MetaText directive class which is used as the default
7 # class (and is a suitable base class) for Directive objects created by
8 # the MetaText Factory object.
9 #
10 # AUTHOR
11 # Andy Wardley <abw@kfs.org>
12 #
13 # COPYRIGHT
14 # Copyright (C) 1996-1998 Andy Wardley. All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the terms of the Perl Artistic Licence.
18 #
19 #----------------------------------------------------------------------------
20 #
21 # $Id: Directive.pm,v 0.4 1998/09/01 12:59:37 abw Exp abw $
22 #
23 #============================================================================
24
25 package Text::MetaText::Directive;
26  
27 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 ▶use vars qw( $VERSION $ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 9 require 5.004;
31  
32  
33  
34 #========================================================================
35 # ----- CONFIGURATION -----
36 #========================================================================
37
38 9 $VERSION = sprintf("%d.%02d", q$Revision: 0.4 $ =~ /(\d+)\.(\d+)/);
39  
40  
41  
42 #========================================================================
43 # ----- PUBLIC METHODS -----
44 #========================================================================
45
46 #========================================================================
47 #
48 # new(\%cfg)
49 #
50 # Module constructor. A reference to a hash array is passed which is
51 # simply blessed into the relevant class and returned. This is an
52 # extremely simplistic construction process which relies on a well-
53 # defined relationship with the Factory class. Derived classes may
54 # easily extend the functionality of the constructor at this point.
55 #
56 # Returns a reference to a newly created Text::MetaText::Directive.
57 # Derived classes should return a reference to a sub-class of
58 # Text::MetaText::Directive or undef on error. If an error condition
59 # occurs, it should be reported using the private $self->_error()
60 # method. This makes the error message available to the calling
61 # factory object via the error() package function.
62 #
63 #========================================================================
64  
65 230 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod undocumented
66 230 my $class = shift;
67 230 my $self = shift;
68 230 my %params = (
69 HAS_CONDITION => [ qw( IF UNLESS ) ],
70 HAS_POSTPROC => [ qw( FORMAT FILTER ) ],
71 );
72 230 my ($key, $value);
73  
74  
75 # check a parameter hash was supplied
76 230 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 230 230
77 0 $self->_error("Directive constructor expects a parameter hash");
78 0 return undef;
79 }
80  
81 # bless the hashref into the required class
82 230 bless $self, $class;
83  
84 # the only thing we do to the new Directive is to examine its internals
85 # and see which optimisation flags we need to set
86 230 ▶ while (($key, $value) = each %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 460 230 690
87 460 foreach (@$value) {
88 $self->{ $key } = 1, last
89 871 ▶ if defined $self->{ $_ };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$_} 53 818 871
90 }
91 }
92  
93 230 $self;
94 }
95  
96  
97  
98 #========================================================================
99 #
100 # error()
101 #
102 # Returns the value of the $ERROR package variable which may be undef
103 # to indicate no current error condition. May be called as a package
104 # function or an object method
105 #
106 #========================================================================
107  
108 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
109 0 return $ERROR;
110 }
111  
112  
113  
114 #========================================================================
115 # ----- PRIVATE METHODS -----
116 #========================================================================
117
118 #========================================================================
119 #
120 # sub _error($errmsg, @params)
121 #
122 # Formats the error message format, $errmsg, and any additional parameters,
123 # @params with sprintf and sets $ERROR package variable with the resulting
124 # string. The package variable, $ERROR, is used rather than an object
125 # member because the error reporting may have to deal with constructor
126 # failures where no object is returned. May be called as a package
127 # function or an object method.
128 #
129 #========================================================================
130  
131 0 ▶sub _error {
Subroutine _error: not called CC=3 SCAR=24.8
132 0 my $self = shift;
133 0 ▶ my $msg = ref($self) ? shift : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
134  
135 0 ▶ $ERROR = defined($msg)
Branch: 1 branch
BranchTrueFalseTotal
defined $msg ? : 0 0 0
136 ? sprintf($msg, @_)
137 : undef;
138 }
139  
140  
141  
142 9 1;
143  
144  
145 =head1 NAME
146  
147 Text::MetaText::Directive - MetaText Directive object class.
148  
149 =head1 SYNOPSIS
150  
151 use Text::MetaText::Directive;
152 my $directive = Text::MetaText::Directive->new(\%params);
153  
154 =head1 DESCRIPTION
155  
156 Objects of the Text::MetaText::Directive class are instantiated by the
157 Text::MetaText::Factory class from within the Text::MetaText module.
158 The Factory and Directive classes can be sub-classed to create a more
159 specific processing system.
160  
161 =head1 AUTHOR
162  
163 Andy Wardley E<lt>abw@kfs.orgE<gt>
164  
165 See also:
166  
167 http://www.kfs.org/~abw/
168  
169 =head1 REVISION
170  
171 $Revision: 0.4 $
172  
173 =head1 COPYRIGHT
174  
175 Copyright (c) 1996-1998 Andy Wardley. All Rights Reserved.
176  
177 This program is free software; you can redistribute it and/or modify it
178 under the terms of the Perl Artistic License.
179  
180 =head1 SEE ALSO
181  
182 For more information, see the main Text::MetaText documentation:
183  
184 perldoc Text::MetaText
185
186 For more information about the author and other Perl development work:
187  
188 http://www.kfs.org/~abw/
189 http://www.kfs.org/~abw/perl/
190 http://www.cre.canon.co.uk/perl/
191  
192 For more information about Perl in general:
193  
194 http://www.perl.com/
195  
196 =cut
197  
198  
« MetaText/Factory.pm ↑ Summary