| 1 | #============================================================================ | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Text::MetaText | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | # Perl 5 module to process template files, featuring variable | ||||||||||||||||||||||||||||
| 7 | # substitution, file inclusion, conditional operations, print | ||||||||||||||||||||||||||||
| 8 | # filters and formatting, etc. | ||||||||||||||||||||||||||||
| 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: MetaText.pm,v 0.22 1998/09/01 11:23:14 abw Exp abw $ | ||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||
| 23 | #============================================================================ | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | package Text::MetaText; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 9 | ▶ | use FileHandle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | use Date::Format; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 9 | ▶ | use vars qw( $VERSION $FACTORY $ERROR ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 9 | ▶ | use Text::MetaText::Factory; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 9 | require 5.004; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | #======================================================================== | ||||||||||||||||||||||||||||
| 39 | # ----- CONFIGURATION ----- | ||||||||||||||||||||||||||||
| 40 | #======================================================================== | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 9 | $VERSION = sprintf("%d.%02d", q$Revision: 0.22 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 43 | 9 | $FACTORY = 'Text::MetaText::Factory'; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # debug level constants (debugging will get nicer one day RSN) | ||||||||||||||||||||||||||||
| 47 | 9 | ▶ | use constant DBGNONE => 0; # no debugging | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 9 | ▶ | use constant DBGINFO => 1; # information message only | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 9 | ▶ | use constant DBGCONF => 2; # configuration details | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 9 | ▶ | use constant DBGPREP => 4; # show pre-processor operations | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 9 | ▶ | use constant DBGPROC => 8; # show process operation | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 9 | ▶ | use constant DBGPOST => 16; # show post-process operation | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 9 | ▶ | use constant DBGDATA => 32; # show data elements (parameters) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 9 | ▶ | use constant DBGCONT => 64; # show content of blocks | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 9 | ▶ | use constant DBGFUNC => 128; # private method calls | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 9 | ▶ | use constant DBGEVAL => 256; # show conditional evaluation steps | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 9 | ▶ | use constant DBGTEST => 512; # test code | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 9 | ▶ | use constant DBGALL => 1023; # all debug information | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 9 | my $DBGNAME = { | |||||||||||||||||||||||||||
| 61 | 'none' => DBGNONE, | ||||||||||||||||||||||||||||
| 62 | 'info' => DBGINFO, | ||||||||||||||||||||||||||||
| 63 | 'config' => DBGCONF, | ||||||||||||||||||||||||||||
| 64 | 'preproc' => DBGPREP, | ||||||||||||||||||||||||||||
| 65 | 'process' => DBGPROC, | ||||||||||||||||||||||||||||
| 66 | 'postproc' => DBGPOST, | ||||||||||||||||||||||||||||
| 67 | 'data' => DBGDATA, | ||||||||||||||||||||||||||||
| 68 | 'content' => DBGCONT, | ||||||||||||||||||||||||||||
| 69 | 'function' => DBGFUNC, | ||||||||||||||||||||||||||||
| 70 | 'evaluate' => DBGEVAL, | ||||||||||||||||||||||||||||
| 71 | 'test' => DBGTEST, | ||||||||||||||||||||||||||||
| 72 | 'all' => DBGALL, | ||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | #======================================================================== | ||||||||||||||||||||||||||||
| 78 | # ----- PUBLIC METHODS ----- | ||||||||||||||||||||||||||||
| 79 | #======================================================================== | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | #======================================================================== | ||||||||||||||||||||||||||||
| 82 | # | ||||||||||||||||||||||||||||
| 83 | # new($cfg) | ||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||
| 85 | # Module constructor. Reference to a hash array containing configuration | ||||||||||||||||||||||||||||
| 86 | # options may be passed as a parameter. This is passed off to | ||||||||||||||||||||||||||||
| 87 | # _configure() for processing. | ||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||
| 89 | # Returns a reference to a newly created Text::MetaText object. | ||||||||||||||||||||||||||||
| 90 | # | ||||||||||||||||||||||||||||
| 91 | #======================================================================== | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 13 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 13 | my $class = shift; | |||||||||||||||||||||||||||
| 95 | 13 | my $self = {}; | |||||||||||||||||||||||||||
| 96 | 13 | bless $self, $class; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 13 | $self->_configure(@_); | |||||||||||||||||||||||||||
| 99 | 13 | return $self; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #======================================================================== | ||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||
| 106 | # process_file($file, \%tags) | ||||||||||||||||||||||||||||
| 107 | # | ||||||||||||||||||||||||||||
| 108 | # Public method for processing files. Calls _parse_file($file) to | ||||||||||||||||||||||||||||
| 109 | # parse and load the file into the symbol table (indexed by $file) | ||||||||||||||||||||||||||||
| 110 | # and then calls $self->_process($file, $tags) to process the symbol | ||||||||||||||||||||||||||||
| 111 | # table entry and generate output. The optional $tags parameter may be | ||||||||||||||||||||||||||||
| 112 | # used to refer to a hash array of pre-defined variables which should be | ||||||||||||||||||||||||||||
| 113 | # used when processing the file. | ||||||||||||||||||||||||||||
| 114 | # | ||||||||||||||||||||||||||||
| 115 | # Returns the result of $self->_process($file, $tags) which may be undef | ||||||||||||||||||||||||||||
| 116 | # to indicate a processing error. May also return undef to indicate a | ||||||||||||||||||||||||||||
| 117 | # parse error. On success, a text string is returned which contains the | ||||||||||||||||||||||||||||
| 118 | # output of the process stage. | ||||||||||||||||||||||||||||
| 119 | # | ||||||||||||||||||||||||||||
| 120 | #======================================================================== | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 85 | ▶ | sub process_file { | ||||||||||||||||||||||||||
|
Subroutine
process_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 85 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 85 | my $file = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 85 | $self->_DEBUG(DBGFUNC, "process_file($file, %s)\n", join(", ", @_)); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # parse the file into the symbol table if it's not already there | ||||||||||||||||||||||||||||
| 130 | 85 | ▶ | unless ($self->_symbol_defined($file)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 30 | ▶ | return undef unless defined $self->_parse_file($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # call _process to do the real processing and implicitly return result | ||||||||||||||||||||||||||||
| 135 | 85 | $self->_process($file, @_); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | #======================================================================== | ||||||||||||||||||||||||||||
| 141 | # | ||||||||||||||||||||||||||||
| 142 | # process_text($text, \%tags) | ||||||||||||||||||||||||||||
| 143 | # | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Public method for processing text strings. Calls _parse_text($text) to | ||||||||||||||||||||||||||||
| 146 | # parse the string and return a reference to an anonymous array, $block, | ||||||||||||||||||||||||||||
| 147 | # which represents the parsed text string, separated by newlines. This | ||||||||||||||||||||||||||||
| 148 | # is then passed to $self->_process($block, @_) along with any other | ||||||||||||||||||||||||||||
| 149 | # parameters passed in to process_text(), such as $tags which is a | ||||||||||||||||||||||||||||
| 150 | # reference to a hash array of pre-defined variables. | ||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | # Returns the result of $self->_process($block, $tags) which may be undef | ||||||||||||||||||||||||||||
| 153 | # to indicate a processing error. May also return undef to indicate a | ||||||||||||||||||||||||||||
| 154 | # parse error. On success, a text string is returned which contains the | ||||||||||||||||||||||||||||
| 155 | # output of the process stage. | ||||||||||||||||||||||||||||
| 156 | # | ||||||||||||||||||||||||||||
| 157 | #======================================================================== | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub process_text { | ||||||||||||||||||||||||||
|
Subroutine
process_text: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 160 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 161 | 0 | my $text = shift; | |||||||||||||||||||||||||||
| 162 | 0 | my $block; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | $self->_DEBUG(DBGFUNC, "process_text($text, ", join(", ", @_), ")\n"); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # parse the text and store the returned block array | ||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return undef unless defined($block = $self->_parse_text($text)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # call _process to do the real processing and implicitly return result | ||||||||||||||||||||||||||||
| 172 | 0 | $self->_process($block, @_); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | #======================================================================== | ||||||||||||||||||||||||||||
| 178 | # | ||||||||||||||||||||||||||||
| 179 | # process($file, \%tags) | ||||||||||||||||||||||||||||
| 180 | # | ||||||||||||||||||||||||||||
| 181 | # Alias for 'process_file(@_)' which is provided for backward | ||||||||||||||||||||||||||||
| 182 | # compatibility with older MetaText versions. | ||||||||||||||||||||||||||||
| 183 | # | ||||||||||||||||||||||||||||
| 184 | #======================================================================== | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 26 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 187 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 188 | 26 | $self->process_file(@_); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | #======================================================================== | ||||||||||||||||||||||||||||
| 194 | # | ||||||||||||||||||||||||||||
| 195 | # declare($input, $name) | ||||||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||||||
| 197 | # Public method which allows text blocks and pre-compiled directive | ||||||||||||||||||||||||||||
| 198 | # arrays to be installed in the symbol table for subsequent use in | ||||||||||||||||||||||||||||
| 199 | # %% INCLUDE <something> %% directives. | ||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||
| 201 | # In the simplest case, $input is a text string (i.e. any scalar) which | ||||||||||||||||||||||||||||
| 202 | # may contain embedded MetaText directives. This is parsed using the | ||||||||||||||||||||||||||||
| 203 | # _parse_text($input, $name) method which creates a parsed directive | ||||||||||||||||||||||||||||
| 204 | # list which is subsequently installed in the symbol table, indexed by | ||||||||||||||||||||||||||||
| 205 | # $name. Subsequent directives of the form "%% INCLUDE $name %%" will | ||||||||||||||||||||||||||||
| 206 | # then correctly resolve the cached contents parsed from the text string. | ||||||||||||||||||||||||||||
| 207 | # | ||||||||||||||||||||||||||||
| 208 | # $input may also be a reference to an array of text strings and/or | ||||||||||||||||||||||||||||
| 209 | # MetaText directive objects. These are instances of the | ||||||||||||||||||||||||||||
| 210 | # Text::MetaText::Directive class, or sub-classes thereof. If you know | ||||||||||||||||||||||||||||
| 211 | # how to instantiate directive objects directly, then you can store | ||||||||||||||||||||||||||||
| 212 | # "pre-compiled" blocks straight into the symbol table using this method. | ||||||||||||||||||||||||||||
| 213 | # This can significantly speed up processing times for complex, | ||||||||||||||||||||||||||||
| 214 | # dynamically contructed blocks by totally elimiating the parsing stage. | ||||||||||||||||||||||||||||
| 215 | # | ||||||||||||||||||||||||||||
| 216 | # The MetaText Directive class will shortly be updated (beyond 0.2) | ||||||||||||||||||||||||||||
| 217 | # to make this process easier. At that point, the Directive documentation | ||||||||||||||||||||||||||||
| 218 | # will updated to better explain this process. In the mean time, don't | ||||||||||||||||||||||||||||
| 219 | # worry if you don't understand this - you're probably not one of the | ||||||||||||||||||||||||||||
| 220 | # two people who specifically needed this feature :-) | ||||||||||||||||||||||||||||
| 221 | # | ||||||||||||||||||||||||||||
| 222 | # Returns 1 if the symbol table entry was correctly defined. If a parse | ||||||||||||||||||||||||||||
| 223 | # error occurs (when parsing a text string), an error is raised and | ||||||||||||||||||||||||||||
| 224 | # undef is returned. | ||||||||||||||||||||||||||||
| 225 | # | ||||||||||||||||||||||||||||
| 226 | #======================================================================== | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 2 | ▶ | sub declare { | ||||||||||||||||||||||||||
|
Subroutine
declare: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 229 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 230 | 2 | my $input = shift; | |||||||||||||||||||||||||||
| 231 | 2 | my $name = shift; | |||||||||||||||||||||||||||
| 232 | 2 | my $ref; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | # is $input a reference of some kind? | ||||||||||||||||||||||||||||
| 235 | 2 | ▶ | if ($ref = ref($input)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # $input may be an array ref of text/directives | ||||||||||||||||||||||||||||
| 238 | 1 | ▶ | $ref eq 'ARRAY' && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | # get a symbol table entry reference | ||||||||||||||||||||||||||||
| 240 | 1 | my $symtabent = $self->_symbol_entry($name); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # clear any existing symbol table entry and push new content | ||||||||||||||||||||||||||||
| 243 | 1 | ▶ | splice(@$symtabent, 0) if scalar @$symtabent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 1 | push(@$symtabent, @$input); | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # no problem | ||||||||||||||||||||||||||||
| 247 | 1 | return 1; | |||||||||||||||||||||||||||
| 248 | }; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # $input may (in the future) be other kinds of refs... | ||||||||||||||||||||||||||||
| 251 | 0 | $self->_error("Invalid input reference passed to declare()"); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | else { | ||||||||||||||||||||||||||||
| 254 | # $input is not a reference so we assume it is text; we call | ||||||||||||||||||||||||||||
| 255 | # _parse_text($input, $name) to handle it but we do *not* | ||||||||||||||||||||||||||||
| 256 | # directly propagate the return value which is a direct reference | ||||||||||||||||||||||||||||
| 257 | # to the symbol table entry; data encapsulation and all that | ||||||||||||||||||||||||||||
| 258 | 1 | ▶ | return $self->_parse_text($input, $name) ? 1 : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | #======================================================================== | ||||||||||||||||||||||||||||
| 265 | # | ||||||||||||||||||||||||||||
| 266 | # error() | ||||||||||||||||||||||||||||
| 267 | # | ||||||||||||||||||||||||||||
| 268 | # Public method returning contents of internal ERROR string. | ||||||||||||||||||||||||||||
| 269 | # | ||||||||||||||||||||||||||||
| 270 | #======================================================================== | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | sub error { | ||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 273 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 0 | return $self->{ ERROR }; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | #======================================================================== | ||||||||||||||||||||||||||||
| 281 | # ----- PRIVATE METHODS ----- | ||||||||||||||||||||||||||||
| 282 | #======================================================================== | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | #======================================================================== | ||||||||||||||||||||||||||||
| 285 | # | ||||||||||||||||||||||||||||
| 286 | # _configure($cfg) | ||||||||||||||||||||||||||||
| 287 | # | ||||||||||||||||||||||||||||
| 288 | # Configuration method which examines the elements in the hash array | ||||||||||||||||||||||||||||
| 289 | # referenced by $cfg and sets the object's internal state accordingly. | ||||||||||||||||||||||||||||
| 290 | # Errors/warnings are reported via $self->_warn(); | ||||||||||||||||||||||||||||
| 291 | # | ||||||||||||||||||||||||||||
| 292 | #======================================================================== | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 13 | ▶ | sub _configure { | ||||||||||||||||||||||||||
|
Subroutine
_configure: called CC=24 SCAR=40.5
| |||||||||||||||||||||||||||||
| 295 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 296 | 13 | my $cfg = shift; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # initialise class data members | ||||||||||||||||||||||||||||
| 300 | 13 | $self->{ SYMTABLE } = {}; | |||||||||||||||||||||||||||
| 301 | 13 | $self->{ LINES } = []; | |||||||||||||||||||||||||||
| 302 | 13 | $self->{ ERROR } = ''; # error string (not ERRORFN!) | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | # set configuration defaults | ||||||||||||||||||||||||||||
| 305 | 13 | $self->{ DEBUGLEVEL } = DBGNONE; # DEBUG mask | |||||||||||||||||||||||||||
| 306 | 13 | $self->{ MAGIC } = [ '%%', '%%' ]; # directive delimiters | |||||||||||||||||||||||||||
| 307 | 13 | $self->{ MAXDEPTH } = 32; # maximum recursion depth | |||||||||||||||||||||||||||
| 308 | 13 | $self->{ LIB } = ""; # library path for INCLUDE | |||||||||||||||||||||||||||
| 309 | 13 | $self->{ ROGUE } = {}; # how to handle rogue directives | |||||||||||||||||||||||||||
| 310 | 13 | $self->{ CASE } = 0; # case sensitivity flag | |||||||||||||||||||||||||||
| 311 | 13 | $self->{ CASEVARS } = {}; # case sensitive variables | |||||||||||||||||||||||||||
| 312 | 13 | $self->{ CHOMP } = 0; # chomp straggling newlines | |||||||||||||||||||||||||||
| 313 | 13 | $self->{ TRIM } = 1; # trim INCLUDE leading/trailing newlines | |||||||||||||||||||||||||||
| 314 | 13 | $self->{ EXECUTE } = 0; # execute SUBST as function? | |||||||||||||||||||||||||||
| 315 | 13 | $self->{ DELIMITER } = ','; # what splits a list? | |||||||||||||||||||||||||||
| 316 | $self->{ FILTER } = { # pre-defined filters | ||||||||||||||||||||||||||||
| 317 | 4 | ▶ | 'sr' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 318 | 4 | ▶ | my $m1 = $_[2] || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[2] || ''
MC/DC0%0/1
$_[2] || ''
$_[2]
Truth table50%1/2
A
A= $_[2]
| |||||||||||||||||||||||||||||
| 319 | 4 | ▶ | my $m2 = $_[3] || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$_[3] || ''
MC/DC100%1/1
$_[3] || ''
$_[3]
Truth table100%2/2
A
A= $_[3]
| |||||||||||||||||||||||||||||
| 320 | 4 | $_[1] =~ s/$m1/$m2/g; | |||||||||||||||||||||||||||
| 321 | 4 | $_[1]; | |||||||||||||||||||||||||||
| 322 | }, | ||||||||||||||||||||||||||||
| 323 | 4 | ▶ | 'escape' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 324 | 4 | ▶ | my $cm = $_[2] || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[2] || ''
MC/DC0%0/1
$_[2] || ''
$_[2]
Truth table50%1/2
A
A= $_[2]
| |||||||||||||||||||||||||||||
| 325 | 4 | $_[1] =~ s/($cm)/\\$1/g; | |||||||||||||||||||||||||||
| 326 | 4 | $_[1]; | |||||||||||||||||||||||||||
| 327 | }, | ||||||||||||||||||||||||||||
| 328 | 13 | }; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | # the config hash array reference, $cfg, may contain a number of | ||||||||||||||||||||||||||||
| 331 | # different config options. These are examined case-insensitively | ||||||||||||||||||||||||||||
| 332 | # (but converted to UPPER CASE when stored) and, depending on the | ||||||||||||||||||||||||||||
| 333 | # option, tested for correctness, manipulated or massaged in some | ||||||||||||||||||||||||||||
| 334 | # way; invalid options generate a warning. | ||||||||||||||||||||||||||||
| 335 | 13 | ▶ | return unless defined $cfg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | # check a hash ref was supplied as $cfg | ||||||||||||||||||||||||||||
| 338 | 11 | ▶ | unless (ref($cfg) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 0 | $self->_warn(ref($self) . "->new expects a hash array reference\n"); | |||||||||||||||||||||||||||
| 340 | 0 | return; | |||||||||||||||||||||||||||
| 341 | }; | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 11 | foreach (keys %$cfg) { | |||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | # set simple config values (converting keyword to UPPER case) | ||||||||||||||||||||||||||||
| 346 | 15 | ▶ | /^(MAXDEPTH|LIB|DELIMITER|CASE|CHOMP|TRIM|EXECUTE)$/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | 7 | $self->{ "\U$_" } = $cfg->{ $_ }; | |||||||||||||||||||||||||||
| 348 | 7 | next; | |||||||||||||||||||||||||||
| 349 | }; | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # add any user-defined print filters to the pre-defined ones | ||||||||||||||||||||||||||||
| 352 | 8 | ▶ | /^FILTER$/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | 0 | my $filter; | |||||||||||||||||||||||||||
| 354 | 0 | foreach $filter (keys %{$cfg->{ $_ }}) { | |||||||||||||||||||||||||||
| 355 | 0 | $self->{ "\U$_" }->{ $filter } = $cfg->{ $_ }->{ $filter }; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | 0 | next; | |||||||||||||||||||||||||||
| 358 | }; | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | # debuglevel is defined as a series of non-word delimited words | ||||||||||||||||||||||||||||
| 361 | # which index into the $DBGNAME hash ref for values | ||||||||||||||||||||||||||||
| 362 | 8 | ▶ | /^DEBUGLEVEL$/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 0 | foreach (split(/\W+/, $cfg->{ $_ })) { | |||||||||||||||||||||||||||
| 364 | $self->_warn("Invalid debug option: $_\n"), next | ||||||||||||||||||||||||||||
| 365 | 0 | ▶ | unless defined($DBGNAME->{ $_ }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | # logically OR in the new debug value | ||||||||||||||||||||||||||||
| 368 | 0 | $self->{ DEBUGLEVEL } |= $DBGNAME->{ $_ }; | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | 0 | next; | |||||||||||||||||||||||||||
| 371 | }; | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | # ROGUE defines how unrecognised (rogue) directives should | ||||||||||||||||||||||||||||
| 374 | # be handled. | ||||||||||||||||||||||||||||
| 375 | 8 | ▶ | /^ROGUE$/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | # create a hash reference of valid ROGUE options and | ||||||||||||||||||||||||||||
| 377 | # print a warning message about invalid options | ||||||||||||||||||||||||||||
| 378 | 3 | foreach my $rogue (split(/\W+/, $cfg->{ $_ })) { | |||||||||||||||||||||||||||
| 379 | 4 | ▶ | if ($rogue =~ /^warn|delete$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | 4 | $self->{ ROGUE }->{ uc $rogue } = 1; | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | else { | ||||||||||||||||||||||||||||
| 383 | 0 | $self->_warn("Invalid rogue option: \L$_\n"); | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | 3 | next; | |||||||||||||||||||||||||||
| 387 | }; | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # CASEVARS are those variables which don't get folded to lower | ||||||||||||||||||||||||||||
| 390 | # case when case sensitivity is turned off. This is useful for | ||||||||||||||||||||||||||||
| 391 | # metapage which likes to define some "system" variables in | ||||||||||||||||||||||||||||
| 392 | # UPPER CASE such as FILETIME, FILENAME, etc. | ||||||||||||||||||||||||||||
| 393 | 5 | ▶ | /^CASEVARS$/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 1 | ▶ | if (ref($cfg->{ $_ }) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 1 | foreach my $var (@{ $cfg->{ $_ } }) { | |||||||||||||||||||||||||||
| 396 | 2 | $self->{ CASEVARS }->{ $var } = 1; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | else { | ||||||||||||||||||||||||||||
| 400 | 0 | $self->_warn("CASEVARS option expects an array ref\n"); | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | 1 | next; | |||||||||||||||||||||||||||
| 403 | }; | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | # MAGIC needs a little processing to convert to a 2 element | ||||||||||||||||||||||||||||
| 406 | # ARRAY ref if a single string was specified (i.e. for both) | ||||||||||||||||||||||||||||
| 407 | 4 | ▶ | /^MAGIC$/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | 0 | ▶ | if (ref($cfg->{ $_ }) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 409 | 0 | $self->{ MAGIC } = $cfg->{ $_ }; | |||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | else { | ||||||||||||||||||||||||||||
| 412 | # create a 2-element array reference | ||||||||||||||||||||||||||||
| 413 | 0 | $self->{ MAGIC } = [ ($cfg->{ $_ }) x 2 ]; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | 0 | next; | |||||||||||||||||||||||||||
| 416 | }; | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | # set ERROR/DEBUG handling function, checking for a CODE reference | ||||||||||||||||||||||||||||
| 419 | # NOTE: error function is stored internally as 'ERRORFN' and not as | ||||||||||||||||||||||||||||
| 420 | # 'ERROR' which is the object error status (backwards compatability). | ||||||||||||||||||||||||||||
| 421 | 4 | ▶ | /^(ERROR|DEBUG)(FN)?$/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | # check this is a code reference | ||||||||||||||||||||||||||||
| 423 | $self->_warn("Invalid \L$_\E function\n"), next | ||||||||||||||||||||||||||||
| 424 | 4 | ▶ | unless ref($cfg->{ $_ }) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | 4 | $self->{ uc $1 . "FN" } = $cfg->{ $_ }; | |||||||||||||||||||||||||||
| 426 | 4 | next; | |||||||||||||||||||||||||||
| 427 | }; | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | # FACTORY must contain a reference to a $FACTORY class or | ||||||||||||||||||||||||||||
| 430 | # derivation of same | ||||||||||||||||||||||||||||
| 431 | 0 | ▶ | /^FACTORY$/i && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | $self->_warn("Invalid factory object"), next | ||||||||||||||||||||||||||||
| 433 | 0 | ▶ | unless UNIVERSAL::isa($cfg->{ $_ }, $FACTORY); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 0 | $self->{ FACTORY } = $cfg->{ $_ }; | |||||||||||||||||||||||||||
| 435 | 0 | next; | |||||||||||||||||||||||||||
| 436 | }; | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | # warn about unrecognised parameter | ||||||||||||||||||||||||||||
| 439 | 0 | $self->_warn("Invalid configuration parameter: $_\n"); | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | # DEBUG code | ||||||||||||||||||||||||||||
| 445 | 11 | ▶ | if ($self->{ DEBUGLEVEL } & DBGCONF) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | 0 | $self->_DEBUG(DBGCONF, "$self Version $VERSION\n"); | |||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | 0 | foreach (keys %$self) { | |||||||||||||||||||||||||||
| 449 | 0 | $self->_DEBUG(DBGDATA, " %-10s => %s\n", $_, $self->{ $_ }); | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | #======================================================================== | ||||||||||||||||||||||||||||
| 458 | # | ||||||||||||||||||||||||||||
| 459 | # _parse_file($file) | ||||||||||||||||||||||||||||
| 460 | # | ||||||||||||||||||||||||||||
| 461 | # Attempts to locate a file with the filename as specified in $file. | ||||||||||||||||||||||||||||
| 462 | # If the filename starts with a '/' or '.', it is assumed to be an absolute | ||||||||||||||||||||||||||||
| 463 | # file path or one relative to the current working directory. In these | ||||||||||||||||||||||||||||
| 464 | # cases, no attempt to look for it outside of its specified location is made. | ||||||||||||||||||||||||||||
| 465 | # Otherwise, the directories specified in the LIB entry in the config hash | ||||||||||||||||||||||||||||
| 466 | # array are searched followed by the current working directory. If the file | ||||||||||||||||||||||||||||
| 467 | # is found, a number of member data items are initialised, the file is | ||||||||||||||||||||||||||||
| 468 | # opened and then _parse($file) is called to parse the file. | ||||||||||||||||||||||||||||
| 469 | # | ||||||||||||||||||||||||||||
| 470 | # Returns the result from _parse($file) or undef on failure. | ||||||||||||||||||||||||||||
| 471 | # | ||||||||||||||||||||||||||||
| 472 | #======================================================================== | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | 30 | ▶ | sub _parse_file { | ||||||||||||||||||||||||||
|
Subroutine
_parse_file: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||
| 475 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 476 | 30 | my $file = shift; | |||||||||||||||||||||||||||
| 477 | 30 | my ($dir, $filepath); | |||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 30 | $self->_DEBUG(DBGFUNC, "_parse_file($file)\n"); | |||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | # default $filepath to $file (may be an absolute path) | ||||||||||||||||||||||||||||
| 484 | 30 | $filepath = $file; | |||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | # file is relative to $self->{ LIB } unless it starts '/' or '.' | ||||||||||||||||||||||||||||
| 487 | 30 | ▶ | if (defined($self->{ LIB }) && $filepath !~ /^[\/\.]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"LIB"} and not $filepath =~ m[^[/\.]]
MC/DC0%0/2
defined $self->{"LIB"} and not $filepath =~ m[^[/\.]]
defined $self->{"LIB"}
not $filepath =~ m[^[/\.]]
Truth table33%1/3
A and not(B)
A= defined $self->{"LIB"}B= not $filepath =~ m[^[/\.]]
| |||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 30 | foreach $dir (split(/[|;:,]/, $self->{ LIB }), '.') { | |||||||||||||||||||||||||||
| 490 | # construct a full file path | ||||||||||||||||||||||||||||
| 491 | 39 | $filepath = $dir; | |||||||||||||||||||||||||||
| 492 | 39 | ▶ | $filepath .= '/' unless ($filepath =~ /\/$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 493 | 39 | $filepath .= $file; | |||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | # test if the file exists | ||||||||||||||||||||||||||||
| 496 | 39 | ▶ | last if -f $filepath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | # open file (may still fail if above loop dropped out the bottom) | ||||||||||||||||||||||||||||
| 501 | 30 | ▶ | unless (defined($self->{ FILE } = new FileHandle $filepath)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 502 | 0 | $self->_error("$filepath: $!"); | |||||||||||||||||||||||||||
| 503 | 0 | return undef; | |||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | 30 | $self->_DEBUG(DBGINFO, "loading file: $filepath\n"); | |||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | # initialise file stats | ||||||||||||||||||||||||||||
| 509 | 30 | $self->{ LINENO } = 0; # no of lines read from _get_line(); | |||||||||||||||||||||||||||
| 510 | 30 | $self->{ PUTBACK } = 0; # no of lines put back via _unget_line(); | |||||||||||||||||||||||||||
| 511 | 30 | $self->{ FILENAME } = $file; | |||||||||||||||||||||||||||
| 512 | 30 | $self->{ FILEPATH } = $filepath; | |||||||||||||||||||||||||||
| 513 | 30 | $self->{ INPUT } = "$file"; # used for error reporting | |||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | # call _parse($file) and implicitly return result | ||||||||||||||||||||||||||||
| 516 | 30 | $self->_parse($file); | |||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | #======================================================================== | ||||||||||||||||||||||||||||
| 522 | # | ||||||||||||||||||||||||||||
| 523 | # _parse_text($text, $symbol) | ||||||||||||||||||||||||||||
| 524 | # | ||||||||||||||||||||||||||||
| 525 | # Initialises the text member data so that _get_line() can read from it | ||||||||||||||||||||||||||||
| 526 | # and then calls _parse() to parse the text contents. If $symbol is | ||||||||||||||||||||||||||||
| 527 | # defined it is used as the symbol name which is then stored in the | ||||||||||||||||||||||||||||
| 528 | # symbol table. If $symbol is undefined, the block remains anonymous. | ||||||||||||||||||||||||||||
| 529 | # | ||||||||||||||||||||||||||||
| 530 | # Returns the result from _parse(). | ||||||||||||||||||||||||||||
| 531 | # | ||||||||||||||||||||||||||||
| 532 | #======================================================================== | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 1 | ▶ | sub _parse_text { | ||||||||||||||||||||||||||
|
Subroutine
_parse_text: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 535 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 536 | 1 | my $text = shift; | |||||||||||||||||||||||||||
| 537 | 1 | my $symbol = shift; # may be undef | |||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | 1 | ▶ | $self->_DEBUG(DBGFUNC, "_parse_text($text, ", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 541 | defined $symbol ? $symbol : "<undef>", ")\n"); | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | # set text string and initialise stats | ||||||||||||||||||||||||||||
| 545 | 1 | $self->{ LINENO } = 0; # no of lines read from _get_line(); | |||||||||||||||||||||||||||
| 546 | 1 | $self->{ PUTBACK } = 0; # no of lines put back via _unget_line(); | |||||||||||||||||||||||||||
| 547 | 1 | $self->{ TEXT } = $text; | |||||||||||||||||||||||||||
| 548 | 1 | $self->{ INPUT } = "text string"; # used for error reporting | |||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | # call _parse() and implicitly return result | ||||||||||||||||||||||||||||
| 551 | 1 | $self->_parse($symbol); | |||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | #======================================================================== | ||||||||||||||||||||||||||||
| 557 | # | ||||||||||||||||||||||||||||
| 558 | # _parse($symbol) | ||||||||||||||||||||||||||||
| 559 | # | ||||||||||||||||||||||||||||
| 560 | # The _parse() method reads the current input stream which may originate | ||||||||||||||||||||||||||||
| 561 | # from a file (_parse_file($file)) or a text string (_parse_text($text)). | ||||||||||||||||||||||||||||
| 562 | # The contents are split into chunks of plain text or MetaText directives | ||||||||||||||||||||||||||||
| 563 | # (enclosed by the MAGIC tokens). Text chunks are pushed directly onto | ||||||||||||||||||||||||||||
| 564 | # an output list, while directives are parsed and blessed into a directive | ||||||||||||||||||||||||||||
| 565 | # class before being pushed out. A reference to the output list is | ||||||||||||||||||||||||||||
| 566 | # returned. If a symbol name is passed as the first parameter to parse(), | ||||||||||||||||||||||||||||
| 567 | # then a corresponding entry in the $self->{ SYMTABLE } hash is created | ||||||||||||||||||||||||||||
| 568 | # to reference this list. | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | # Processing continues until EOF is reached or an %% END(BLOCK|IF)? %% | ||||||||||||||||||||||||||||
| 571 | # directive is encountered. | ||||||||||||||||||||||||||||
| 572 | # | ||||||||||||||||||||||||||||
| 573 | # Blocks encountered that are bounded by a matched pair of %% BLOCK name %% | ||||||||||||||||||||||||||||
| 574 | # ... %% ENDBLOCK %% directives will cause a recursive call to | ||||||||||||||||||||||||||||
| 575 | # $self->_parse($blockname) to be made to handle the block definition for | ||||||||||||||||||||||||||||
| 576 | # the sub-block. Block definitions can theoretically be nested indefinately | ||||||||||||||||||||||||||||
| 577 | # although in practice, the process ends when an upper recursion limit is | ||||||||||||||||||||||||||||
| 578 | # reached ($self->{ MAXDEPTH }). To this effect, $depth is used to | ||||||||||||||||||||||||||||
| 579 | # internally indicate the current recursion depth to each instance. | ||||||||||||||||||||||||||||
| 580 | # | ||||||||||||||||||||||||||||
| 581 | #======================================================================== | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | 50 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=27 SCAR=34.5
| |||||||||||||||||||||||||||||
| 584 | 50 | my $self = shift; | |||||||||||||||||||||||||||
| 585 | 50 | my $symbol = shift; # may be undef - i.e. anonymous symbol | |||||||||||||||||||||||||||
| 586 | 50 | ▶ | my $depth = shift || 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 1
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 587 | 50 | my ($magic1, $magic2); | |||||||||||||||||||||||||||
| 588 | 50 | my ($line, $nextline); | |||||||||||||||||||||||||||
| 589 | 50 | my ($symtabent, $factory, $directive); | |||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | 50 | ▶ | $self->_DEBUG(DBGFUNC, "_parse(%s)\n", defined $symbol ? $symbol : ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | # check for excessive recursion | ||||||||||||||||||||||||||||
| 596 | 50 | ▶ | if ($depth > $self->{ MAXDEPTH }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 597 | 0 | $self->_error("Maximum recursion exceeded in _parse()"); | |||||||||||||||||||||||||||
| 598 | 0 | return undef; | |||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | # get a local copy of the MAGIC symbols for efficiency | ||||||||||||||||||||||||||||
| 602 | 50 | ($magic1, $magic2) = @{ $self->{ MAGIC } }; | |||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | # get a symbol table entry reference (an undefined $symbol causes | ||||||||||||||||||||||||||||
| 605 | # an anonymous array ref to be returned). | ||||||||||||||||||||||||||||
| 606 | 50 | $symtabent = $self->_symbol_entry($symbol); | |||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | # clear any existing symbol table entry; this doesn't affect caching, | ||||||||||||||||||||||||||||
| 609 | # BTW because _parse() only gets called when reload is necessary | ||||||||||||||||||||||||||||
| 610 | 50 | ▶ | splice(@$symtabent, 0) if scalar @$symtabent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | # get a reference to the factory object used to create directives | ||||||||||||||||||||||||||||
| 613 | 50 | ▶ | return undef unless $factory = $self->_factory(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | # | ||||||||||||||||||||||||||||
| 617 | # main parsing loop begineth here | ||||||||||||||||||||||||||||
| 618 | # | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | 50 | ▶ | READLINE: while (defined($line = $self->_get_line())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | # look to see if there is a directive in the line | ||||||||||||||||||||||||||||
| 623 | 772 | ▶ | while ($line =~ / | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 624 | (.*?) # anything preceeding a directive | ||||||||||||||||||||||||||||
| 625 | $magic1 # opening directive marker | ||||||||||||||||||||||||||||
| 626 | \s* # whitespace | ||||||||||||||||||||||||||||
| 627 | (.*?) # directive contents | ||||||||||||||||||||||||||||
| 628 | \s* # whitespace | ||||||||||||||||||||||||||||
| 629 | ( | ||||||||||||||||||||||||||||
| 630 | ($magic2) # closing directive marker | ||||||||||||||||||||||||||||
| 631 | (.*) # rest of the line | ||||||||||||||||||||||||||||
| 632 | )? # directive may not be terminated | ||||||||||||||||||||||||||||
| 633 | $ # EOL so it all gets eaten | ||||||||||||||||||||||||||||
| 634 | /sx) { | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | # | ||||||||||||||||||||||||||||
| 638 | # if the directive terminating symbol ($magic2) wasn't | ||||||||||||||||||||||||||||
| 639 | # found in the line then it suggests that the directive | ||||||||||||||||||||||||||||
| 640 | # continues onto the next line, so we append the next | ||||||||||||||||||||||||||||
| 641 | # line and try again. | ||||||||||||||||||||||||||||
| 642 | # | ||||||||||||||||||||||||||||
| 643 | 330 | ▶ | unless ($4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 644 | # if we can't read another line, tack on the | ||||||||||||||||||||||||||||
| 645 | # magic token to avoid a dangling directive | ||||||||||||||||||||||||||||
| 646 | 101 | ▶ | unless (defined($nextline = $self->_get_line())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 647 | 0 | $nextline = $magic2; | |||||||||||||||||||||||||||
| 648 | 0 | $self->_warn("Closing directive tag missing\n"); | |||||||||||||||||||||||||||
| 649 | } | ||||||||||||||||||||||||||||
| 650 | 101 | chomp($line); | |||||||||||||||||||||||||||
| 651 | # add a space and the next line | ||||||||||||||||||||||||||||
| 652 | 101 | $line .= " $nextline"; | |||||||||||||||||||||||||||
| 653 | 101 | next; | |||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | # | ||||||||||||||||||||||||||||
| 657 | # at this point, we have a line that has a complete directive | ||||||||||||||||||||||||||||
| 658 | # ($2) enclosed within it, perhaps with leading ($1) and | ||||||||||||||||||||||||||||
| 659 | # trailing ($5) text | ||||||||||||||||||||||||||||
| 660 | # | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | # push any preceding text into the output list | ||||||||||||||||||||||||||||
| 663 | 229 | ▶ | push(@$symtabent, $1) if length $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | # anything coming after the directive gets re-queued. | ||||||||||||||||||||||||||||
| 666 | # CHOMP can be set to remove straggling newlines | ||||||||||||||||||||||||||||
| 667 | $self->_unget_line($5) | ||||||||||||||||||||||||||||
| 668 | 229 | ▶ | unless $self->{ CHOMP } && $5 eq "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"CHOMP"} and $5 eq "\n"
MC/DC100%2/2
$self->{"CHOMP"} and $5 eq "\n"
$self->{"CHOMP"}
$5 eq "\n"
Truth table100%3/3
A and B
A= $self->{"CHOMP"}B= $5 eq "\n"
| |||||||||||||||||||||||||||||
| 669 | 229 | $line = ""; | |||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | 229 | ▶ | if (defined $2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | # get the create a new Text::MetaText::Directive object | ||||||||||||||||||||||||||||
| 674 | 229 | $directive = $factory->create_directive($2); | |||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | # check everything worked OK. eval? bletch! | ||||||||||||||||||||||||||||
| 677 | 229 | ▶ | unless (defined $directive) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 678 | 0 | $self->_parse_error($factory->error()); | |||||||||||||||||||||||||||
| 679 | 0 | return undef; | |||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | 229 | my $tt = "Directive created:\n"; | |||||||||||||||||||||||||||
| 683 | 229 | foreach (keys %$directive) { | |||||||||||||||||||||||||||
| 684 | $tt .= sprintf(" %-16s => %s\n", | ||||||||||||||||||||||||||||
| 685 | 1027 | $_, $directive->{ $_ }); | |||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||
| 687 | 229 | $tt .= " params:\n"; | |||||||||||||||||||||||||||
| 688 | 229 | ▶ | foreach (keys %{ $directive->{ PARAMS } || { } }) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$directive->{'PARAMS'} || {}
MC/DC0%0/1
$directive->{'PARAMS'} || {}
$directive->{'PARAMS'}
Truth table50%1/2
A
A= $directive->{'PARAMS'}
| |||||||||||||||||||||||||||||
| 689 | $tt .= sprintf(" %-16s => %s\n", | ||||||||||||||||||||||||||||
| 690 | 118 | $_, $directive->{ PARAMS }->{ $_ }); | |||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||
| 692 | 229 | $self->_DEBUG(DBGTEST, $tt); | |||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | # | ||||||||||||||||||||||||||||
| 695 | # some specialist processing required depending on | ||||||||||||||||||||||||||||
| 696 | # $directive->{ TYPE } | ||||||||||||||||||||||||||||
| 697 | # | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | # END(BLOCK|IF)? marks the end of a defined block | ||||||||||||||||||||||||||||
| 700 | 229 | ▶ | $directive->{ TYPE } =~ /^END(BLOCK|IF)?$/ && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | # save a copy of the tag that ended this block | ||||||||||||||||||||||||||||
| 703 | # so that the calling method can check it | ||||||||||||||||||||||||||||
| 704 | 19 | $self->{ ENDTAG } = $directive->{ TYPE }; | |||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | # return the symbol table list | ||||||||||||||||||||||||||||
| 707 | 19 | return $symtabent; | |||||||||||||||||||||||||||
| 708 | }; | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | # BLOCK directive defines a sub-block | ||||||||||||||||||||||||||||
| 711 | 210 | ▶ | $directive->{ TYPE } eq 'BLOCK' && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | # clear ENDTAG data | ||||||||||||||||||||||||||||
| 714 | 19 | $self->{ ENDTAG } = ""; | |||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||
| 716 | # we recursively call $self->_parse() to parse the | ||||||||||||||||||||||||||||
| 717 | # block and return a reference to the symbol table | ||||||||||||||||||||||||||||
| 718 | # entry; | ||||||||||||||||||||||||||||
| 719 | my $block = $self->_parse( | ||||||||||||||||||||||||||||
| 720 | 19 | $directive->{ IDENTIFIER }, $depth + 1); | |||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||
| 722 | # check comething was returned | ||||||||||||||||||||||||||||
| 723 | 19 | ▶ | return undef unless defined $block; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | # test that the directive that terminated the block | ||||||||||||||||||||||||||||
| 726 | # was END(BLOCK)? | ||||||||||||||||||||||||||||
| 727 | 19 | ▶ | unless ($self->{ ENDTAG } =~ /^END(BLOCK)?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 728 | 0 | $self->_parse_error("ENDBLOCK expected"); | |||||||||||||||||||||||||||
| 729 | 0 | return undef; | |||||||||||||||||||||||||||
| 730 | } | ||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||
| 732 | # if the 'TRIM' option is defined, we should remove | ||||||||||||||||||||||||||||
| 733 | # any leading newline and the final newline from the | ||||||||||||||||||||||||||||
| 734 | # last line. | ||||||||||||||||||||||||||||
| 735 | 19 | ▶ | if (defined $directive->{ TRIM } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 736 | ? $directive->{ TRIM } | ||||||||||||||||||||||||||||
| 737 | : $self->{ TRIM }) { | ||||||||||||||||||||||||||||
| 738 | 15 | ▶ | shift @$block | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 739 | if $block->[0] eq "\n"; | ||||||||||||||||||||||||||||
| 740 | 15 | chomp($block->[ $#{ $block } ]); | |||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||
| 743 | # if the 'PRINT' option was defined, we convert the | ||||||||||||||||||||||||||||
| 744 | # BLOCK directive to an INCLUDE and push it onto the | ||||||||||||||||||||||||||||
| 745 | # symbol table so that it gets processed and a copy | ||||||||||||||||||||||||||||
| 746 | # of the BLOCK gets pushed to the output | ||||||||||||||||||||||||||||
| 747 | 19 | ▶ | if (defined($directive->{ PRINT })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 748 | 0 | $directive->{ TYPE } = 'INCLUDE'; | |||||||||||||||||||||||||||
| 749 | 0 | push(@$symtabent, $directive); | |||||||||||||||||||||||||||
| 750 | } | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | # loop to avoid directive getting (re-)pushed below | ||||||||||||||||||||||||||||
| 753 | 19 | next; | |||||||||||||||||||||||||||
| 754 | }; | ||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||
| 756 | # push the directive onto the symbol table list | ||||||||||||||||||||||||||||
| 757 | 191 | push(@$symtabent, $directive); | |||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||
| 759 | } # if (defined($2)) | ||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | } # while ($line =~ ... | ||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | # anything remaining in $line must be plain text | ||||||||||||||||||||||||||||
| 764 | 753 | ▶ | push(@$symtabent, $line) if length($line); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | } # READLINE: while... | ||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||
| 768 | # return a reference to the 'compiled' symbol table entry | ||||||||||||||||||||||||||||
| 769 | 31 | $symtabent; | |||||||||||||||||||||||||||
| 770 | } | ||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | #======================================================================== | ||||||||||||||||||||||||||||
| 775 | # | ||||||||||||||||||||||||||||
| 776 | # _process($symbol, \%tags, $depth) | ||||||||||||||||||||||||||||
| 777 | # | ||||||||||||||||||||||||||||
| 778 | # $symbol is a scalar holding the name of a known symbol or a reference | ||||||||||||||||||||||||||||
| 779 | # to an array which contains the nodes for an anonymous symbol. In the | ||||||||||||||||||||||||||||
| 780 | # former case, the symbol is referenced from the symbol table by calling | ||||||||||||||||||||||||||||
| 781 | # $self->_symbol_entry($symbol). In the latter case, the method simply | ||||||||||||||||||||||||||||
| 782 | # iterates through the elements of the $symbol array reference. | ||||||||||||||||||||||||||||
| 783 | # | ||||||||||||||||||||||||||||
| 784 | # Each element in the symbol table entry array is expected to be a simple | ||||||||||||||||||||||||||||
| 785 | # scalar containing plain text or a MetaText directive - an instance of | ||||||||||||||||||||||||||||
| 786 | # the Text::MetaText::Directive class. Plain text is pushed straight | ||||||||||||||||||||||||||||
| 787 | # through to an output queue. Directves are processed according to | ||||||||||||||||||||||||||||
| 788 | # their type (e.g. INCLUDE, DEFINE, SUBST, etc) and the resulting output | ||||||||||||||||||||||||||||
| 789 | # is pushed onto the output queue. | ||||||||||||||||||||||||||||
| 790 | # | ||||||||||||||||||||||||||||
| 791 | # The method returns a concatenation of the output list or undef on | ||||||||||||||||||||||||||||
| 792 | # error. | ||||||||||||||||||||||||||||
| 793 | # | ||||||||||||||||||||||||||||
| 794 | #======================================================================== | ||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | 85 | ▶ | sub _process { | ||||||||||||||||||||||||||
|
Subroutine
_process: called CC=28 SCAR=34.6
| |||||||||||||||||||||||||||||
| 797 | 85 | my $self = shift; | |||||||||||||||||||||||||||
| 798 | 85 | my $symbol = shift; | |||||||||||||||||||||||||||
| 799 | 85 | ▶ | my $tags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 800 | 85 | ▶ | my $depth = shift || 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 1
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 801 | 85 | my ($symtabent, $factory, $directive, $item, $type, $space); | |||||||||||||||||||||||||||
| 802 | 85 | my ($ident); | |||||||||||||||||||||||||||
| 803 | 85 | my $proctext; | |||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | 85 | my @output = (); | |||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||
| 808 | 85 | $self->_DEBUG(DBGFUNC, "_process($symbol, $tags, $depth)\n"); | |||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | # check for excessive recursion | ||||||||||||||||||||||||||||
| 812 | 85 | ▶ | if ($depth > $self->{ MAXDEPTH }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 813 | 0 | $self->_error("Maximum recursion exceeded"); | |||||||||||||||||||||||||||
| 814 | 0 | return undef; | |||||||||||||||||||||||||||
| 815 | } | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | # $symbol may be a reference to an anonymous block array... | ||||||||||||||||||||||||||||
| 818 | 85 | ▶ | if (ref($symbol) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 819 | 0 | $symtabent = $symbol; | |||||||||||||||||||||||||||
| 820 | } | ||||||||||||||||||||||||||||
| 821 | # ...or a named symbol which may or may not have been pre-parsed | ||||||||||||||||||||||||||||
| 822 | else { | ||||||||||||||||||||||||||||
| 823 | # check the symbol has an entry in the symbol table | ||||||||||||||||||||||||||||
| 824 | 85 | ▶ | unless ($self->_symbol_defined($symbol)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 825 | 0 | $self->_error("$symbol: no such block defined"); | |||||||||||||||||||||||||||
| 826 | 0 | return undef; | |||||||||||||||||||||||||||
| 827 | } | ||||||||||||||||||||||||||||
| 828 | 85 | $symtabent = $self->_symbol_entry($symbol); | |||||||||||||||||||||||||||
| 829 | } | ||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||
| 831 | # get a reference to the factory object and call directive_type() | ||||||||||||||||||||||||||||
| 832 | # to determine the kind of Directive objects it creates | ||||||||||||||||||||||||||||
| 833 | 85 | ▶ | return undef unless $factory = $self->_factory(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 834 | 85 | $directive = $factory->directive_type(); | |||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||
| 837 | # | ||||||||||||||||||||||||||||
| 838 | # The symbol table entry is an array reference passed explicitly in | ||||||||||||||||||||||||||||
| 839 | # $symbol or retrieved by calling $self->_symbol_entry($symbol); | ||||||||||||||||||||||||||||
| 840 | # Each element in the array can be either a plain text string or an | ||||||||||||||||||||||||||||
| 841 | # instance of the directive class created by the factory object. | ||||||||||||||||||||||||||||
| 842 | # The former represent normal text blocks in the processed file, the | ||||||||||||||||||||||||||||
| 843 | # latter represent pre-parsed MetaText directives (see _parse()) that | ||||||||||||||||||||||||||||
| 844 | # have been created by the factory object. The factory provides the | ||||||||||||||||||||||||||||
| 845 | # directive_type() method for determining the class type of these | ||||||||||||||||||||||||||||
| 846 | # objects. A directive will contain some of the following elements, | ||||||||||||||||||||||||||||
| 847 | # based on the directive type and other data defined in the directive | ||||||||||||||||||||||||||||
| 848 | # block: | ||||||||||||||||||||||||||||
| 849 | # | ||||||||||||||||||||||||||||
| 850 | # $directive->{ TYPE } # directive type: INCLUDE, DEFINE, etc | ||||||||||||||||||||||||||||
| 851 | # $directive->{ IDENTIFIER } # target, i.e. INCLUDE <filename> | ||||||||||||||||||||||||||||
| 852 | # $directive->{ PARAMS } # hash ref of variables defined | ||||||||||||||||||||||||||||
| 853 | # $directive->{ PARAMSTR } # original parameter string | ||||||||||||||||||||||||||||
| 854 | # $directive->{ IF } # an "if=..." conditional | ||||||||||||||||||||||||||||
| 855 | # $directive->{ UNLESS } # ditto "unless=..." | ||||||||||||||||||||||||||||
| 856 | # $directive->{ DELIMITER } # delimiter string (see _evaluate()) | ||||||||||||||||||||||||||||
| 857 | # $directive->{ FILTER } # print filter name and params | ||||||||||||||||||||||||||||
| 858 | # $directive->{ FORMAT } # print format | ||||||||||||||||||||||||||||
| 859 | # | ||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||
| 861 | # process each each line from the block | ||||||||||||||||||||||||||||
| 862 | 85 | foreach $item (@$symtabent) { | |||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||
| 864 | # get rid of the non-directive cases first... | ||||||||||||||||||||||||||||
| 865 | 876 | ▶ | unless (UNIVERSAL::isa($item, $directive)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||
| 867 | # return content if we find the end-of-content marker | ||||||||||||||||||||||||||||
| 868 | 656 | ▶ | return join("", @output) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 869 | if $item =~ /^__(MT)?END__$/; | ||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||
| 871 | # not a directive - so just push output and loop | ||||||||||||||||||||||||||||
| 872 | 650 | push(@output, $item); | |||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||
| 874 | 650 | next; | |||||||||||||||||||||||||||
| 875 | } | ||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||
| 878 | # examine any conditionals (if/unless) if defined | ||||||||||||||||||||||||||||
| 879 | 220 | ▶ | if ($item->{ HAS_CONDITION }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||
| 881 | # test any "if=<condition>" statement... | ||||||||||||||||||||||||||||
| 882 | 42 | ▶ | if (defined $item->{ IF }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 883 | my $result = $self->_evaluate($item->{ IF }, $tags, | ||||||||||||||||||||||||||||
| 884 | 40 | ▶ | $item->{ DELIMITER } || $self->{ DELIMITER }); | ||||||||||||||||||||||||||
|
Condition33%1/3
$item->{'DELIMITER'} || $self->{'DELIMITER'}
MC/DC0%0/2
$item->{'DELIMITER'} || $self->{'DELIMITER'}
$item->{'DELIMITER'}
$self->{'DELIMITER'}
Truth table33%1/3
A || B
A= $item->{'DELIMITER'}B= $self->{'DELIMITER'}
| |||||||||||||||||||||||||||||
| 885 | 40 | ▶ | next unless defined($result) && $result > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $result and $result > 0
MC/DC50%1/2
defined $result and $result > 0
defined $result
$result > 0
Truth table66%2/3
A and B
A= defined $resultB= $result > 0
| |||||||||||||||||||||||||||||
| 886 | } | ||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||
| 888 | # ...and/or any "unless=<condition>" statement | ||||||||||||||||||||||||||||
| 889 | 22 | ▶ | if (defined $item->{ UNLESS }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 890 | my $result = $self->_evaluate($item->{ UNLESS }, $tags, | ||||||||||||||||||||||||||||
| 891 | 2 | ▶ | $item->{ DELIMITER } || $self->{ DELIMITER }); | ||||||||||||||||||||||||||
|
Condition33%1/3
$item->{'DELIMITER'} || $self->{'DELIMITER'}
MC/DC0%0/2
$item->{'DELIMITER'} || $self->{'DELIMITER'}
$item->{'DELIMITER'}
$self->{'DELIMITER'}
Truth table33%1/3
A || B
A= $item->{'DELIMITER'}B= $self->{'DELIMITER'}
| |||||||||||||||||||||||||||||
| 892 | 2 | ▶ | next if defined($result) && $result != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $result and $result != 0
MC/DC50%1/2
defined $result and $result != 0
defined $result
$result != 0
Truth table66%2/3
A and B
A= defined $resultB= $result != 0
| |||||||||||||||||||||||||||||
| 893 | } | ||||||||||||||||||||||||||||
| 894 | } | ||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||
| 896 | |||||||||||||||||||||||||||||
| 897 | # we take a copy of the directive TYPE and IDENTIFIER (operand) | ||||||||||||||||||||||||||||
| 898 | 199 | $type = $item->{ TYPE }; | |||||||||||||||||||||||||||
| 899 | 199 | $ident = $item->{ IDENTIFIER }; | |||||||||||||||||||||||||||
| 900 | |||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||
| 902 | #------------------------------------ | ||||||||||||||||||||||||||||
| 903 | # switch ($type) | ||||||||||||||||||||||||||||
| 904 | # | ||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||
| 906 | 199 | ▶ | $type eq 'DEFINE' && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||
| 908 | # $tags is a hash array ref passed in to _process(). We must | ||||||||||||||||||||||||||||
| 909 | # clone it before modification in case we should accidentally | ||||||||||||||||||||||||||||
| 910 | # update the caller's hash. | ||||||||||||||||||||||||||||
| 911 | 37 | $tags = { %$tags }; | |||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||
| 913 | # merge in parameters defined within the INCLUDE directive | ||||||||||||||||||||||||||||
| 914 | 37 | $self->_integrate_params($tags, $item->{ PARAMS }); | |||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||
| 916 | 37 | next; | |||||||||||||||||||||||||||
| 917 | }; | ||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||
| 919 | 162 | ▶ | $type eq 'INCLUDE' && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||
| 921 | # an INCLUDE identifier is allowed to contain variable | ||||||||||||||||||||||||||||
| 922 | # references which must be interpolated. | ||||||||||||||||||||||||||||
| 923 | 59 | $ident = $self->_interpolate($ident, $tags); | |||||||||||||||||||||||||||
| 924 | |||||||||||||||||||||||||||||
| 925 | # clone the existing tags | ||||||||||||||||||||||||||||
| 926 | 59 | my $newtags = { %$tags }; | |||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||
| 928 | # merge in parameters defined within the INCLUDE directive | ||||||||||||||||||||||||||||
| 929 | 59 | $self->_integrate_params($newtags, $item->{ PARAMS }); | |||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||
| 931 | # process the INCLUDE'd symbol and check return | ||||||||||||||||||||||||||||
| 932 | 59 | $proctext = $self->process_file($ident, $newtags, $depth + 1); | |||||||||||||||||||||||||||
| 933 | 59 | ▶ | return undef unless defined $proctext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||
| 935 | # push text onto output list, post-processing it along the way | ||||||||||||||||||||||||||||
| 936 | # if $self->{ HAS_POSTPROC } is true (i.e. has filter/format) | ||||||||||||||||||||||||||||
| 937 | push(@output, | ||||||||||||||||||||||||||||
| 938 | $item->{ HAS_POSTPROC } | ||||||||||||||||||||||||||||
| 939 | 59 | ▶ | ? $self->_post_process($item, $proctext) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 940 | : $proctext); | ||||||||||||||||||||||||||||
| 941 | |||||||||||||||||||||||||||||
| 942 | 59 | next; | |||||||||||||||||||||||||||
| 943 | }; | ||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||
| 945 | 103 | ▶ | $type eq 'SUBST' && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||
| 947 | # call _substitute to handle token substitution | ||||||||||||||||||||||||||||
| 948 | 103 | $proctext = $self->_substitute($item, $tags); | |||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||
| 950 | 103 | ▶ | if (defined($proctext)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 951 | $proctext = $self->_post_process($item, $proctext) | ||||||||||||||||||||||||||||
| 952 | 98 | ▶ | if $item->{ HAS_POSTPROC }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 953 | } | ||||||||||||||||||||||||||||
| 954 | else { | ||||||||||||||||||||||||||||
| 955 | # unrecognised token | ||||||||||||||||||||||||||||
| 956 | $self->_warn("Unrecognised token: $item->{ IDENTIFIER }\n") | ||||||||||||||||||||||||||||
| 957 | 5 | ▶ | if defined $self->{ ROGUE }->{ WARN }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||
| 959 | # resolve nothing if 'delete' is defined as a ROGUE option | ||||||||||||||||||||||||||||
| 960 | $proctext = $self->{ ROGUE }->{ DELETE } | ||||||||||||||||||||||||||||
| 961 | ? "" | ||||||||||||||||||||||||||||
| 962 | : $self->{ MAGIC }->[ 0 ] # rebuild directive | ||||||||||||||||||||||||||||
| 963 | . " " | ||||||||||||||||||||||||||||
| 964 | . $item->{ PARAMSTR } | ||||||||||||||||||||||||||||
| 965 | . " " | ||||||||||||||||||||||||||||
| 966 | 5 | ▶ | . $self->{ MAGIC }->[ 1 ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 967 | } | ||||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||
| 969 | 103 | push(@output, $proctext); | |||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||
| 971 | 103 | next; | |||||||||||||||||||||||||||
| 972 | }; | ||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||
| 974 | # default: invalid directive; this shouldn't happen | ||||||||||||||||||||||||||||
| 975 | 0 | $self->_warn("Unrecognise directive: $type\n") | |||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||
| 977 | # | ||||||||||||||||||||||||||||
| 978 | # switch ($type) | ||||||||||||||||||||||||||||
| 979 | #------------------------------------ | ||||||||||||||||||||||||||||
| 980 | } | ||||||||||||||||||||||||||||
| 981 | |||||||||||||||||||||||||||||
| 982 | # join output tokens and return as a single line | ||||||||||||||||||||||||||||
| 983 | 79 | join("", @output); | |||||||||||||||||||||||||||
| 984 | } | ||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||
| 986 | |||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||
| 988 | #======================================================================== | ||||||||||||||||||||||||||||
| 989 | # | ||||||||||||||||||||||||||||
| 990 | # _get_line() | ||||||||||||||||||||||||||||
| 991 | # | ||||||||||||||||||||||||||||
| 992 | # Returns the next pending line of text to be processed from the input | ||||||||||||||||||||||||||||
| 993 | # file or text string. If there are no pending lines already in the | ||||||||||||||||||||||||||||
| 994 | # queue, it reads a line of text from the file handle, $self->{ FILE }. | ||||||||||||||||||||||||||||
| 995 | # If $self->{ FILE } is undefined, it looks at $self->{ TEXT }, splits | ||||||||||||||||||||||||||||
| 996 | # the contents into lines and pushes them onto the pending line list. | ||||||||||||||||||||||||||||
| 997 | # The next pending line in the list can then be returned. | ||||||||||||||||||||||||||||
| 998 | # | ||||||||||||||||||||||||||||
| 999 | # Return a string representing the next input line or undef if no further | ||||||||||||||||||||||||||||
| 1000 | # lines are available (at EOF for example). | ||||||||||||||||||||||||||||
| 1001 | # | ||||||||||||||||||||||||||||
| 1002 | #======================================================================== | ||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||
| 1004 | 904 | ▶ | sub _get_line { | ||||||||||||||||||||||||||
|
Subroutine
_get_line: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 1005 | 904 | my $self = shift; | |||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||
| 1008 | $self->_DEBUG(DBGFUNC, "_get_line() (%s #%d)\n", | ||||||||||||||||||||||||||||
| 1009 | 904 | $self->{ INPUT }, $self->{ LINENO } + 1); | |||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||
| 1012 | # if there are no lines pending, we try to add some to the queue | ||||||||||||||||||||||||||||
| 1013 | 904 | ▶ | unless (@{ $self->{ LINES } }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1014 | |||||||||||||||||||||||||||||
| 1015 | 679 | ▶ | if (defined $self->{ FILE }) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 1016 | # read from the file | ||||||||||||||||||||||||||||
| 1017 | 647 | push(@{ $self->{ LINES } }, $self->{ FILE }->getline()); | |||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||
| 1019 | # close file if done | ||||||||||||||||||||||||||||
| 1020 | 647 | ▶ | $self->{ FILE } = undef if $self->{ FILE }->eof(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1021 | } | ||||||||||||||||||||||||||||
| 1022 | elsif (defined $self->{ TEXT }) { | ||||||||||||||||||||||||||||
| 1023 | # split from the text line | ||||||||||||||||||||||||||||
| 1024 | 1 | push(@{ $self->{ LINES } }, split(/^/m, $self->{ TEXT })); | |||||||||||||||||||||||||||
| 1025 | 1 | $self->{ TEXT } = undef; | |||||||||||||||||||||||||||
| 1026 | } | ||||||||||||||||||||||||||||
| 1027 | |||||||||||||||||||||||||||||
| 1028 | # no default | ||||||||||||||||||||||||||||
| 1029 | } | ||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||
| 1031 | # LINENO is incremented to indicate that another line has been read, | ||||||||||||||||||||||||||||
| 1032 | # unless PUTBACK indicates that there are requeued lines. | ||||||||||||||||||||||||||||
| 1033 | 904 | ▶ | if ($self->{ PUTBACK }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1034 | 223 | $self->{ PUTBACK }--; | |||||||||||||||||||||||||||
| 1035 | } | ||||||||||||||||||||||||||||
| 1036 | else { | ||||||||||||||||||||||||||||
| 1037 | 681 | $self->{ LINENO }++; | |||||||||||||||||||||||||||
| 1038 | } | ||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||
| 1040 | # return the next token (may be undef to indicate end of stream) | ||||||||||||||||||||||||||||
| 1041 | 904 | return shift(@{ $self->{ LINES } }); | |||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||
| 1043 | } | ||||||||||||||||||||||||||||
| 1044 | |||||||||||||||||||||||||||||
| 1045 | |||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||
| 1047 | #======================================================================== | ||||||||||||||||||||||||||||
| 1048 | # | ||||||||||||||||||||||||||||
| 1049 | # _unget_line($line) | ||||||||||||||||||||||||||||
| 1050 | # | ||||||||||||||||||||||||||||
| 1051 | # Unshifts the specified line, $line, onto the front of the pending | ||||||||||||||||||||||||||||
| 1052 | # lines queue. Does nothing if $line is undefined. Effectively the | ||||||||||||||||||||||||||||
| 1053 | # complement of _get_line(). The PUTBACK variable variable is | ||||||||||||||||||||||||||||
| 1054 | # incremented. The _get_line() method uses this as an indication that | ||||||||||||||||||||||||||||
| 1055 | # the line is re-queued and decrements PUTBACK instead of incrementing | ||||||||||||||||||||||||||||
| 1056 | # LINENO as per usual. | ||||||||||||||||||||||||||||
| 1057 | # | ||||||||||||||||||||||||||||
| 1058 | #======================================================================== | ||||||||||||||||||||||||||||
| 1059 | |||||||||||||||||||||||||||||
| 1060 | 223 | ▶ | sub _unget_line { | ||||||||||||||||||||||||||
|
Subroutine
_unget_line: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1061 | 223 | my $self = shift; | |||||||||||||||||||||||||||
| 1062 | 223 | my $line = shift; | |||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||
| 1064 | |||||||||||||||||||||||||||||
| 1065 | 223 | ▶ | return unless defined $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||
| 1067 | 223 | my $safeline; | |||||||||||||||||||||||||||
| 1068 | 223 | ($safeline = $line) =~ s/%/%%/g; | |||||||||||||||||||||||||||
| 1069 | $self->_DEBUG(DBGFUNC, "_unget_line(\"$safeline\") (#%d)\n", | ||||||||||||||||||||||||||||
| 1070 | 223 | $self->{ LINENO } - 1); | |||||||||||||||||||||||||||
| 1071 | |||||||||||||||||||||||||||||
| 1072 | # increment PUTBACK to indicate there are re-queued lines | ||||||||||||||||||||||||||||
| 1073 | 223 | $self->{ PUTBACK }++; | |||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||
| 1075 | # unshift (defined) line onto front of list | ||||||||||||||||||||||||||||
| 1076 | 223 | unshift(@{ $self->{ LINES } }, $line); | |||||||||||||||||||||||||||
| 1077 | } | ||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||
| 1080 | |||||||||||||||||||||||||||||
| 1081 | #======================================================================== | ||||||||||||||||||||||||||||
| 1082 | # | ||||||||||||||||||||||||||||
| 1083 | # _factory() | ||||||||||||||||||||||||||||
| 1084 | # | ||||||||||||||||||||||||||||
| 1085 | # Returns a reference to the factory object stored in $self->{ FACTORY }. | ||||||||||||||||||||||||||||
| 1086 | # If this is undefined, an attempt is made to instantiate a factory | ||||||||||||||||||||||||||||
| 1087 | # object from the default class, $FACTORY, which is then stored in the | ||||||||||||||||||||||||||||
| 1088 | # $self->{ FACTORY } hash entry. | ||||||||||||||||||||||||||||
| 1089 | # | ||||||||||||||||||||||||||||
| 1090 | # Returns a reference to the factory object. On failure, undef is returned | ||||||||||||||||||||||||||||
| 1091 | # and a warning is issued via _warn(). | ||||||||||||||||||||||||||||
| 1092 | # | ||||||||||||||||||||||||||||
| 1093 | #======================================================================== | ||||||||||||||||||||||||||||
| 1094 | |||||||||||||||||||||||||||||
| 1095 | 135 | ▶ | sub _factory { | ||||||||||||||||||||||||||
|
Subroutine
_factory: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 1096 | 135 | my $self = shift; | |||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||
| 1098 | |||||||||||||||||||||||||||||
| 1099 | # create a default factory if one doesn't already exist | ||||||||||||||||||||||||||||
| 1100 | 135 | ▶ | unless (defined $self->{ FACTORY }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1101 | # $FACTORY is the default factory package | ||||||||||||||||||||||||||||
| 1102 | 13 | ▶ | $self->{ FACTORY } = $FACTORY->new() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1103 | or $self->_error( | ||||||||||||||||||||||||||||
| 1104 | "Factory construction failed: " | ||||||||||||||||||||||||||||
| 1105 | . "<factory error>" | ||||||||||||||||||||||||||||
| 1106 | ); | ||||||||||||||||||||||||||||
| 1107 | } | ||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||
| 1109 | # return factory reference | ||||||||||||||||||||||||||||
| 1110 | 135 | $self->{ FACTORY }; | |||||||||||||||||||||||||||
| 1111 | } | ||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||
| 1113 | |||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||
| 1115 | #======================================================================== | ||||||||||||||||||||||||||||
| 1116 | # | ||||||||||||||||||||||||||||
| 1117 | # _symbol_name($symbol) | ||||||||||||||||||||||||||||
| 1118 | # | ||||||||||||||||||||||||||||
| 1119 | # Returns the name by which $symbol might be referenced in the symbol | ||||||||||||||||||||||||||||
| 1120 | # table. Applies case folding (to lower case) unless CASE sensitivity | ||||||||||||||||||||||||||||
| 1121 | # is set. | ||||||||||||||||||||||||||||
| 1122 | # | ||||||||||||||||||||||||||||
| 1123 | #======================================================================== | ||||||||||||||||||||||||||||
| 1124 | |||||||||||||||||||||||||||||
| 1125 | 306 | ▶ | sub _symbol_name { | ||||||||||||||||||||||||||
|
Subroutine
_symbol_name: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 1126 | 306 | my $self = shift; | |||||||||||||||||||||||||||
| 1127 | 306 | my $symbol = shift; | |||||||||||||||||||||||||||
| 1128 | |||||||||||||||||||||||||||||
| 1129 | |||||||||||||||||||||||||||||
| 1130 | 306 | $self->_DEBUG(DBGFUNC, "_symbol_name($symbol)\n"); | |||||||||||||||||||||||||||
| 1131 | |||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||
| 1133 | # convert symbol to lower case unless CASE sensitivity is set | ||||||||||||||||||||||||||||
| 1134 | 306 | ▶ | $symbol = lc $symbol unless $self->{ CASE }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1135 | |||||||||||||||||||||||||||||
| 1136 | 306 | return $symbol; | |||||||||||||||||||||||||||
| 1137 | } | ||||||||||||||||||||||||||||
| 1138 | |||||||||||||||||||||||||||||
| 1139 | |||||||||||||||||||||||||||||
| 1140 | |||||||||||||||||||||||||||||
| 1141 | #======================================================================== | ||||||||||||||||||||||||||||
| 1142 | # | ||||||||||||||||||||||||||||
| 1143 | # _symbol_defined($symbol) | ||||||||||||||||||||||||||||
| 1144 | # | ||||||||||||||||||||||||||||
| 1145 | # Returns 1 if the symbol, $symbol, is defined in the symbol table or | ||||||||||||||||||||||||||||
| 1146 | # 0 if not. | ||||||||||||||||||||||||||||
| 1147 | # | ||||||||||||||||||||||||||||
| 1148 | #======================================================================== | ||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||
| 1150 | 170 | ▶ | sub _symbol_defined { | ||||||||||||||||||||||||||
|
Subroutine
_symbol_defined: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1151 | 170 | my $self = shift; | |||||||||||||||||||||||||||
| 1152 | 170 | my $symbol = shift; | |||||||||||||||||||||||||||
| 1153 | |||||||||||||||||||||||||||||
| 1154 | |||||||||||||||||||||||||||||
| 1155 | 170 | $self->_DEBUG(DBGFUNC, "_symbol_defined($symbol)\n"); | |||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||
| 1157 | |||||||||||||||||||||||||||||
| 1158 | # call _symbol_name() to apply any name munging | ||||||||||||||||||||||||||||
| 1159 | 170 | $symbol = $self->_symbol_name($symbol); | |||||||||||||||||||||||||||
| 1160 | |||||||||||||||||||||||||||||
| 1161 | # return 1 or 0 based on existence of symbol table entry | ||||||||||||||||||||||||||||
| 1162 | 170 | ▶ | return exists $self->{ SYMTABLE }->{ $symbol } ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1163 | } | ||||||||||||||||||||||||||||
| 1164 | |||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||
| 1167 | #======================================================================== | ||||||||||||||||||||||||||||
| 1168 | # | ||||||||||||||||||||||||||||
| 1169 | # _symbol_entry($symbol) | ||||||||||||||||||||||||||||
| 1170 | # | ||||||||||||||||||||||||||||
| 1171 | # Returns a reference to the symbol table entry for $symbol. If there | ||||||||||||||||||||||||||||
| 1172 | # is no corresponding symbol currently loaded in the table, the symbol | ||||||||||||||||||||||||||||
| 1173 | # table entry is initiated to an empty array reference, [], and that | ||||||||||||||||||||||||||||
| 1174 | # value is returned. This list can then be filled, via the reference, | ||||||||||||||||||||||||||||
| 1175 | # to populate the symbol table entry. The symbol name, $symbol, may be | ||||||||||||||||||||||||||||
| 1176 | # converted to lower case (via _symbol_name($symbol)) unless case | ||||||||||||||||||||||||||||
| 1177 | # sensitivity ($self->{ CASE }) is set. | ||||||||||||||||||||||||||||
| 1178 | # | ||||||||||||||||||||||||||||
| 1179 | # Returns a reference to the array that represents the symbol table | ||||||||||||||||||||||||||||
| 1180 | # entry for the specified entry. | ||||||||||||||||||||||||||||
| 1181 | # | ||||||||||||||||||||||||||||
| 1182 | #======================================================================== | ||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||
| 1184 | 136 | ▶ | sub _symbol_entry { | ||||||||||||||||||||||||||
|
Subroutine
_symbol_entry: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 1185 | 136 | my $self = shift; | |||||||||||||||||||||||||||
| 1186 | 136 | my $symbol = shift; | |||||||||||||||||||||||||||
| 1187 | |||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||
| 1189 | 136 | ▶ | $self->_DEBUG(DBGFUNC, "_symbol_entry(%s)\n", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1190 | defined $symbol ? $symbol : "<undef>"); | ||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||
| 1192 | |||||||||||||||||||||||||||||
| 1193 | # an undefined symbol gets an anonymous array | ||||||||||||||||||||||||||||
| 1194 | 136 | ▶ | return [] unless defined $symbol; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1195 | |||||||||||||||||||||||||||||
| 1196 | # determine the real symbol name accounting for case folding | ||||||||||||||||||||||||||||
| 1197 | 136 | $symbol = $self->_symbol_name($symbol); | |||||||||||||||||||||||||||
| 1198 | |||||||||||||||||||||||||||||
| 1199 | # create empty table entry for a new symbol | ||||||||||||||||||||||||||||
| 1200 | $self->{ SYMTABLE }->{ $symbol } = [] | ||||||||||||||||||||||||||||
| 1201 | 136 | ▶ | unless defined $self->{ SYMTABLE }->{ $symbol }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1202 | |||||||||||||||||||||||||||||
| 1203 | # return reference to symbol table entry | ||||||||||||||||||||||||||||
| 1204 | 136 | $self->{ SYMTABLE }->{ $symbol }; | |||||||||||||||||||||||||||
| 1205 | } | ||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||
| 1207 | |||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||
| 1209 | #======================================================================== | ||||||||||||||||||||||||||||
| 1210 | # | ||||||||||||||||||||||||||||
| 1211 | # _variable_name($variable) | ||||||||||||||||||||||||||||
| 1212 | # | ||||||||||||||||||||||||||||
| 1213 | # Returns the name by which $symbol might be referenced. Removes any | ||||||||||||||||||||||||||||
| 1214 | # extraneous leading '$' and folds to lower case unless CASE sensitivity | ||||||||||||||||||||||||||||
| 1215 | # is set. | ||||||||||||||||||||||||||||
| 1216 | # | ||||||||||||||||||||||||||||
| 1217 | # Returns the (perhaps modified) variable name. | ||||||||||||||||||||||||||||
| 1218 | # | ||||||||||||||||||||||||||||
| 1219 | #======================================================================== | ||||||||||||||||||||||||||||
| 1220 | |||||||||||||||||||||||||||||
| 1221 | 213 | ▶ | sub _variable_name { | ||||||||||||||||||||||||||
|
Subroutine
_variable_name: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 1222 | 213 | my $self = shift; | |||||||||||||||||||||||||||
| 1223 | 213 | my $variable = shift; | |||||||||||||||||||||||||||
| 1224 | |||||||||||||||||||||||||||||
| 1225 | |||||||||||||||||||||||||||||
| 1226 | 213 | $self->_DEBUG(DBGFUNC, "_variable_name($variable)\n"); | |||||||||||||||||||||||||||
| 1227 | |||||||||||||||||||||||||||||
| 1228 | |||||||||||||||||||||||||||||
| 1229 | # strip leading '$' | ||||||||||||||||||||||||||||
| 1230 | 213 | $variable =~ s/^\$//; | |||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||
| 1232 | # convert symbol to lower case unless CASE sensitivity is set | ||||||||||||||||||||||||||||
| 1233 | 213 | ▶ | $variable = lc $variable unless $self->{ CASE }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||
| 1235 | 213 | return $variable; | |||||||||||||||||||||||||||
| 1236 | } | ||||||||||||||||||||||||||||
| 1237 | |||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||
| 1240 | #======================================================================== | ||||||||||||||||||||||||||||
| 1241 | # | ||||||||||||||||||||||||||||
| 1242 | # _variable_value($variable, $tags) | ||||||||||||||||||||||||||||
| 1243 | # | ||||||||||||||||||||||||||||
| 1244 | # Returns the value associated with the variable as named in $variable. | ||||||||||||||||||||||||||||
| 1245 | # $variable may be modified (by _variable_name()) which removes any | ||||||||||||||||||||||||||||
| 1246 | # leading '$' and folding case unless $self->{ CASE } is set. The | ||||||||||||||||||||||||||||
| 1247 | # resulting variable name is then used to index into $tags to return | ||||||||||||||||||||||||||||
| 1248 | # the associated value. | ||||||||||||||||||||||||||||
| 1249 | # | ||||||||||||||||||||||||||||
| 1250 | # Returns the value from $tags associated with $variable or undef if not | ||||||||||||||||||||||||||||
| 1251 | # defined. | ||||||||||||||||||||||||||||
| 1252 | # | ||||||||||||||||||||||||||||
| 1253 | #======================================================================== | ||||||||||||||||||||||||||||
| 1254 | |||||||||||||||||||||||||||||
| 1255 | 123 | ▶ | sub _variable_value { | ||||||||||||||||||||||||||
|
Subroutine
_variable_value: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 1256 | 123 | my $self = shift; | |||||||||||||||||||||||||||
| 1257 | 123 | my $variable = shift; | |||||||||||||||||||||||||||
| 1258 | 123 | my $tags = shift; | |||||||||||||||||||||||||||
| 1259 | |||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||
| 1261 | 123 | $self->_DEBUG(DBGFUNC, "_variable_value($variable, $tags)\n"); | |||||||||||||||||||||||||||
| 1262 | |||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||
| 1264 | # examine the CASEVARS which lists vars not for CASE folding | ||||||||||||||||||||||||||||
| 1265 | return $tags->{ $variable } | ||||||||||||||||||||||||||||
| 1266 | if (defined $self->{ CASEVARS }->{ $variable } | ||||||||||||||||||||||||||||
| 1267 | 123 | ▶ | && defined $tags->{ $variable }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"CASEVARS"}{$variable} and defined $tags->{$variable}
MC/DC50%1/2
defined $self->{"CASEVARS"}{$variable} and defined $tags->{$variable}
defined $self->{"CASEVARS"}{$variable}
defined $tags->{$variable}
Truth table66%2/3
A and B
A= defined $self->{"CASEVARS"}{$variable}B= defined $tags->{$variable}
| |||||||||||||||||||||||||||||
| 1268 | |||||||||||||||||||||||||||||
| 1269 | # special case(s) | ||||||||||||||||||||||||||||
| 1270 | 122 | ▶ | return time() if $variable eq 'TIME'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1271 | |||||||||||||||||||||||||||||
| 1272 | # apply any case folding rules to the variable name | ||||||||||||||||||||||||||||
| 1273 | 120 | $variable = $self->_variable_name($variable); | |||||||||||||||||||||||||||
| 1274 | |||||||||||||||||||||||||||||
| 1275 | # return the associated value | ||||||||||||||||||||||||||||
| 1276 | 120 | return $tags->{ $variable }; | |||||||||||||||||||||||||||
| 1277 | } | ||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||
| 1281 | #======================================================================== | ||||||||||||||||||||||||||||
| 1282 | # | ||||||||||||||||||||||||||||
| 1283 | # _interpolate($expr, $tags) | ||||||||||||||||||||||||||||
| 1284 | # | ||||||||||||||||||||||||||||
| 1285 | # Examines the string expression, $expr, and attempts to replace any | ||||||||||||||||||||||||||||
| 1286 | # elements within the string that relate to key names in the hash table | ||||||||||||||||||||||||||||
| 1287 | # referenced by $tags. A simple "$variable" subsititution is identified | ||||||||||||||||||||||||||||
| 1288 | # when separated by non-word characters | ||||||||||||||||||||||||||||
| 1289 | # | ||||||||||||||||||||||||||||
| 1290 | # e.g. "foo/$bar/baz" => "foo/" . $tags->{'bar'} . "/baz" | ||||||||||||||||||||||||||||
| 1291 | # | ||||||||||||||||||||||||||||
| 1292 | # Ambiguous variable names can be explicitly resolved using braces as per | ||||||||||||||||||||||||||||
| 1293 | # Unix shell syntax. | ||||||||||||||||||||||||||||
| 1294 | # | ||||||||||||||||||||||||||||
| 1295 | # e.g. "foo${bar}baz" => "foo" . $tags{'bar'} . "baz" | ||||||||||||||||||||||||||||
| 1296 | # | ||||||||||||||||||||||||||||
| 1297 | # The function returns a newly constructed string. If $expr is a reference | ||||||||||||||||||||||||||||
| 1298 | # to a scalar, the original scalar is modified and also returned. | ||||||||||||||||||||||||||||
| 1299 | # | ||||||||||||||||||||||||||||
| 1300 | #======================================================================== | ||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||
| 1302 | 152 | ▶ | sub _interpolate { | ||||||||||||||||||||||||||
|
Subroutine
_interpolate: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 1303 | 152 | my $self = shift; | |||||||||||||||||||||||||||
| 1304 | 152 | my $expr = shift; | |||||||||||||||||||||||||||
| 1305 | 152 | ▶ | my $tags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 1306 | 152 | my ($s1, $s2); | |||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||
| 1308 | |||||||||||||||||||||||||||||
| 1309 | 152 | $self->_DEBUG(DBGFUNC, "_interpolate($expr, $tags)\n"); | |||||||||||||||||||||||||||
| 1310 | |||||||||||||||||||||||||||||
| 1311 | |||||||||||||||||||||||||||||
| 1312 | # if a reference is passed, work on the original, otherwise take a copy | ||||||||||||||||||||||||||||
| 1313 | 152 | ▶ | my $work = ref($expr) eq 'SCALAR' ? $expr : \$expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1314 | |||||||||||||||||||||||||||||
| 1315 | # look for a "$identifier" or "${identifier}" and substitute | ||||||||||||||||||||||||||||
| 1316 | # Note that we save $1 and $2 because they may get trounced during | ||||||||||||||||||||||||||||
| 1317 | # the call to $self->_variable_value() | ||||||||||||||||||||||||||||
| 1318 | 152 | $$work =~ s/ ( \$ \{? ([\w\.]+) \}? ) / | |||||||||||||||||||||||||||
| 1319 | 20 | ($s1, $s2) = ($1, $2); | |||||||||||||||||||||||||||
| 1320 | 20 | ▶ | defined ($s2 = $self->_variable_value($2, $tags)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1321 | ? $s2 | ||||||||||||||||||||||||||||
| 1322 | : $s1; | ||||||||||||||||||||||||||||
| 1323 | /gex; | ||||||||||||||||||||||||||||
| 1324 | |||||||||||||||||||||||||||||
| 1325 | # return modified string | ||||||||||||||||||||||||||||
| 1326 | 152 | $$work; | |||||||||||||||||||||||||||
| 1327 | } | ||||||||||||||||||||||||||||
| 1328 | |||||||||||||||||||||||||||||
| 1329 | |||||||||||||||||||||||||||||
| 1330 | |||||||||||||||||||||||||||||
| 1331 | #======================================================================== | ||||||||||||||||||||||||||||
| 1332 | # | ||||||||||||||||||||||||||||
| 1333 | # _integrate_params($tags, $params, $lookup) | ||||||||||||||||||||||||||||
| 1334 | # | ||||||||||||||||||||||||||||
| 1335 | # Attempts to incorporate all the variables in the $params hash array | ||||||||||||||||||||||||||||
| 1336 | # reference into the current tagset referenced by $tags. Any embedded | ||||||||||||||||||||||||||||
| 1337 | # variable references in the $params values will be interpolated using | ||||||||||||||||||||||||||||
| 1338 | # the values in the $lookup hash. If $lookup is undefined, the $tags | ||||||||||||||||||||||||||||
| 1339 | # hash is used. | ||||||||||||||||||||||||||||
| 1340 | # | ||||||||||||||||||||||||||||
| 1341 | # e.g. | ||||||||||||||||||||||||||||
| 1342 | # if $params->{'foo'} = 'aaa/$bar/bbb' | ||||||||||||||||||||||||||||
| 1343 | # then $tags->{'foo'} = 'aaa' . $lookup->{'bar'} . 'bbb' | ||||||||||||||||||||||||||||
| 1344 | # | ||||||||||||||||||||||||||||
| 1345 | #======================================================================== | ||||||||||||||||||||||||||||
| 1346 | |||||||||||||||||||||||||||||
| 1347 | 100 | ▶ | sub _integrate_params { | ||||||||||||||||||||||||||
|
Subroutine
_integrate_params: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 1348 | 100 | my $self = shift; | |||||||||||||||||||||||||||
| 1349 | 100 | ▶ | my $tags = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 1350 | 100 | ▶ | my $params = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 1351 | 100 | ▶ | my $lookup = shift || $tags; | ||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $tags
MC/DC0%0/2
shift() || $tags
shift()
$tags
Truth table66%2/3
A || B
A= shift()B= $tags
| |||||||||||||||||||||||||||||
| 1352 | 100 | my ($v, $variable, $value); | |||||||||||||||||||||||||||
| 1353 | |||||||||||||||||||||||||||||
| 1354 | |||||||||||||||||||||||||||||
| 1355 | 100 | $self->_DEBUG(DBGFUNC, "_integrate_params($tags, $params, $lookup)\n"); | |||||||||||||||||||||||||||
| 1356 | |||||||||||||||||||||||||||||
| 1357 | |||||||||||||||||||||||||||||
| 1358 | # iterate through each variable in $params | ||||||||||||||||||||||||||||
| 1359 | 100 | foreach $v (keys %$params) { | |||||||||||||||||||||||||||
| 1360 | |||||||||||||||||||||||||||||
| 1361 | # get the real variable name | ||||||||||||||||||||||||||||
| 1362 | 93 | $variable = $self->_variable_name($v); | |||||||||||||||||||||||||||
| 1363 | |||||||||||||||||||||||||||||
| 1364 | # interpolate any variable values in the parameter value | ||||||||||||||||||||||||||||
| 1365 | 93 | $value = $self->_interpolate($params->{ $v }, $lookup); | |||||||||||||||||||||||||||
| 1366 | |||||||||||||||||||||||||||||
| 1367 | # copy variable and value into new tagset | ||||||||||||||||||||||||||||
| 1368 | 93 | $tags->{ $variable } = $value | |||||||||||||||||||||||||||
| 1369 | } | ||||||||||||||||||||||||||||
| 1370 | } | ||||||||||||||||||||||||||||
| 1371 | |||||||||||||||||||||||||||||
| 1372 | |||||||||||||||||||||||||||||
| 1373 | |||||||||||||||||||||||||||||
| 1374 | #======================================================================== | ||||||||||||||||||||||||||||
| 1375 | # | ||||||||||||||||||||||||||||
| 1376 | # _substitute($directive, $tags) | ||||||||||||||||||||||||||||
| 1377 | # | ||||||||||||||||||||||||||||
| 1378 | # Examines the SUBST directive referenced by $directive and looks to | ||||||||||||||||||||||||||||
| 1379 | # see if the variable to which it refers ($directive->{ IDENTIFIER }) | ||||||||||||||||||||||||||||
| 1380 | # exists as a key in the hash table referenced by $tags. | ||||||||||||||||||||||||||||
| 1381 | # | ||||||||||||||||||||||||||||
| 1382 | # If a relevant hash entry does not exist and $self->{ EXECUTE } is set | ||||||||||||||||||||||||||||
| 1383 | # to a true value, _substitute attempts to run the directive name as a | ||||||||||||||||||||||||||||
| 1384 | # class method, allowing derived (sub) classes to define member functions | ||||||||||||||||||||||||||||
| 1385 | # that get called automagically by the base class. If $self->{ EXECUTE } | ||||||||||||||||||||||||||||
| 1386 | # has a value > 1, it attempts to run a function in the main package with | ||||||||||||||||||||||||||||
| 1387 | # the same name as the identifier. If all that fails, undef is returned. | ||||||||||||||||||||||||||||
| 1388 | # | ||||||||||||||||||||||||||||
| 1389 | #======================================================================== | ||||||||||||||||||||||||||||
| 1390 | |||||||||||||||||||||||||||||
| 1391 | 103 | ▶ | sub _substitute { | ||||||||||||||||||||||||||
|
Subroutine
_substitute: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||
| 1392 | 103 | my $self = shift; | |||||||||||||||||||||||||||
| 1393 | 103 | my $directive = shift; | |||||||||||||||||||||||||||
| 1394 | 103 | my $tags = shift; | |||||||||||||||||||||||||||
| 1395 | 103 | my $ident = $directive->{ IDENTIFIER }; | |||||||||||||||||||||||||||
| 1396 | 103 | my ($value, $fn); | |||||||||||||||||||||||||||
| 1397 | |||||||||||||||||||||||||||||
| 1398 | |||||||||||||||||||||||||||||
| 1399 | 103 | $self->_DEBUG(DBGFUNC, "_substitute($directive, $tags)\n"); | |||||||||||||||||||||||||||
| 1400 | |||||||||||||||||||||||||||||
| 1401 | |||||||||||||||||||||||||||||
| 1402 | # get the variable value if it is defined | ||||||||||||||||||||||||||||
| 1403 | 103 | ▶ | return $value | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1404 | if defined ($value = $self->_variable_value($ident, $tags)); | ||||||||||||||||||||||||||||
| 1405 | |||||||||||||||||||||||||||||
| 1406 | # nothing more to do unless EXECUTE is true | ||||||||||||||||||||||||||||
| 1407 | return undef | ||||||||||||||||||||||||||||
| 1408 | 9 | ▶ | unless $self->{ EXECUTE }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1409 | |||||||||||||||||||||||||||||
| 1410 | # extract the original parameter string | ||||||||||||||||||||||||||||
| 1411 | 4 | ▶ | my $prmstr = $directive->{ PARAMSTR } || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$directive->{'PARAMSTR'} || ''
MC/DC0%0/1
$directive->{'PARAMSTR'} || ''
$directive->{'PARAMSTR'}
Truth table50%1/2
A
A= $directive->{'PARAMSTR'}
| |||||||||||||||||||||||||||||
| 1412 | 4 | my $prmhash = { }; | |||||||||||||||||||||||||||
| 1413 | |||||||||||||||||||||||||||||
| 1414 | # create a new set of directive tags, interpolating any embedded vars | ||||||||||||||||||||||||||||
| 1415 | 4 | $self->_integrate_params($prmhash, $directive->{ PARAMS }, $tags); | |||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||
| 1417 | # execute $ident class method if EXECUTE is defined and $ident exists | ||||||||||||||||||||||||||||
| 1418 | 4 | ▶ | if ($self->{ EXECUTE } && $self->can($ident)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"EXECUTE"} and $self->can($ident)
MC/DC50%1/2
$self->{"EXECUTE"} and $self->can($ident)
$self->{"EXECUTE"}
$self->can($ident)
Truth table66%2/3
A and B
A= $self->{"EXECUTE"}B= $self->can($ident)
| |||||||||||||||||||||||||||||
| 1419 | 2 | $self->_DEBUG(DBGINFO, "executing $self->$ident\n"); | |||||||||||||||||||||||||||
| 1420 | 2 | return $self->$ident($prmhash, $prmstr) | |||||||||||||||||||||||||||
| 1421 | } | ||||||||||||||||||||||||||||
| 1422 | |||||||||||||||||||||||||||||
| 1423 | # if EXECUTE is set > 1, we try to run it as a function in the main | ||||||||||||||||||||||||||||
| 1424 | # package. We examine the main symbol table to see if the function | ||||||||||||||||||||||||||||
| 1425 | # exists, otherwise we return undef. | ||||||||||||||||||||||||||||
| 1426 | |||||||||||||||||||||||||||||
| 1427 | 2 | ▶ | return undef unless $self->{ EXECUTE } > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1428 | |||||||||||||||||||||||||||||
| 1429 | # get a function reference from the main symbol table | ||||||||||||||||||||||||||||
| 1430 | 2 | local *glob = $main::{ $ident }; | |||||||||||||||||||||||||||
| 1431 | return undef | ||||||||||||||||||||||||||||
| 1432 | 2 | ▶ | unless defined($fn = *glob{ CODE }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1433 | |||||||||||||||||||||||||||||
| 1434 | 2 | $self->_DEBUG(DBGINFO, "executing main::$ident\n"); | |||||||||||||||||||||||||||
| 1435 | |||||||||||||||||||||||||||||
| 1436 | # execute the function and implicitly return result | ||||||||||||||||||||||||||||
| 1437 | 2 | &{ $fn }($prmhash, $prmstr); | |||||||||||||||||||||||||||
| 1438 | } | ||||||||||||||||||||||||||||
| 1439 | |||||||||||||||||||||||||||||
| 1440 | |||||||||||||||||||||||||||||
| 1441 | |||||||||||||||||||||||||||||
| 1442 | #======================================================================== | ||||||||||||||||||||||||||||
| 1443 | # | ||||||||||||||||||||||||||||
| 1444 | # _evaluate($expr, \%tags, $delimiter) | ||||||||||||||||||||||||||||
| 1445 | # | ||||||||||||||||||||||||||||
| 1446 | # Evaluates the specified expression, $expr, using the token values in | ||||||||||||||||||||||||||||
| 1447 | # the hash array referenced by $tags. The $delimiter parameter may also | ||||||||||||||||||||||||||||
| 1448 | # be passed to over-ride the default delimiter ($self->{ DELIMITER }) | ||||||||||||||||||||||||||||
| 1449 | # which is used when splitting 'in' lists for evalutation | ||||||||||||||||||||||||||||
| 1450 | # (e.g. if="name in Tom,Dick,Harry"). | ||||||||||||||||||||||||||||
| 1451 | # | ||||||||||||||||||||||||||||
| 1452 | # Returns 1 if the expression evaluates true, 0 if it evaluates false. | ||||||||||||||||||||||||||||
| 1453 | # On error (e.g. a badly formed expression), undef is returned. | ||||||||||||||||||||||||||||
| 1454 | # | ||||||||||||||||||||||||||||
| 1455 | # NOTE: This method is ugly, slow and buggy. For most uses, it will do | ||||||||||||||||||||||||||||
| 1456 | # the job admirably, but don't necessarily trust it to do 100% what you | ||||||||||||||||||||||||||||
| 1457 | # expect if your expressions start to get very complicated. In | ||||||||||||||||||||||||||||
| 1458 | # particular, multiple nested parenthesis may not evaluate with the | ||||||||||||||||||||||||||||
| 1459 | # correct precedence, or indeed at all. The method has to parse and | ||||||||||||||||||||||||||||
| 1460 | # evaluate the $expr string every time it is run. This will start to | ||||||||||||||||||||||||||||
| 1461 | # slow your processing down if you do a lot of conditional tests. In | ||||||||||||||||||||||||||||
| 1462 | # the future, it is likely to be compiled down to an intermediate form | ||||||||||||||||||||||||||||
| 1463 | # to improve execution speed. | ||||||||||||||||||||||||||||
| 1464 | # | ||||||||||||||||||||||||||||
| 1465 | #======================================================================== | ||||||||||||||||||||||||||||
| 1466 | |||||||||||||||||||||||||||||
| 1467 | 54 | ▶ | sub _evaluate { | ||||||||||||||||||||||||||
|
Subroutine
_evaluate: called CC=26 SCAR=35.6
| |||||||||||||||||||||||||||||
| 1468 | 54 | my $self = shift; | |||||||||||||||||||||||||||
| 1469 | 54 | my $expr = shift; | |||||||||||||||||||||||||||
| 1470 | 54 | my $tags = shift; | |||||||||||||||||||||||||||
| 1471 | 54 | ▶ | my $delim = shift || $self->{ DELIMITER }; | ||||||||||||||||||||||||||
|
Condition66%2/3
shift() || $self->{'DELIMITER'}
MC/DC0%0/2
shift() || $self->{'DELIMITER'}
shift()
$self->{'DELIMITER'}
Truth table66%2/3
A || B
A= shift()B= $self->{'DELIMITER'}
| |||||||||||||||||||||||||||||
| 1472 | 54 | my ($lhs, $rhs, $sub, $op, $result); | |||||||||||||||||||||||||||
| 1473 | |||||||||||||||||||||||||||||
| 1474 | # save a copy of the original expression for debug purposes | ||||||||||||||||||||||||||||
| 1475 | 54 | my $original = $expr; | |||||||||||||||||||||||||||
| 1476 | |||||||||||||||||||||||||||||
| 1477 | # a hash table of comparison operators and associated functions | ||||||||||||||||||||||||||||
| 1478 | my $compare = { | ||||||||||||||||||||||||||||
| 1479 | 4 | ▶ | '==' => sub { $_[0] eq $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1480 | 2 | ▶ | '=' => sub { $_[0] eq $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1481 | 2 | ▶ | '!=' => sub { $_[0] ne $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1482 | 8 | ▶ | '>=' => sub { $_[0] ge $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1483 | 0 | ▶ | '<=' => sub { $_[0] le $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1484 | 8 | ▶ | '>' => sub { $_[0] gt $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1485 | 4 | ▶ | '<' => sub { $_[0] lt $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1486 | 0 | ▶ | '=~' => sub { $_[0] =~ /$_[1]/ }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1487 | 0 | ▶ | '!~' => sub { $_[0] !~ /$_[1]/ }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1488 | 0 | ▶ | 'in' => sub { grep(/^$_[0]$/, split(/$delim/, $_[1])) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1489 | 54 | }; | |||||||||||||||||||||||||||
| 1490 | # define a regex to match the comparison keys; note that alpha words | ||||||||||||||||||||||||||||
| 1491 | # (\w+) must be protected by "\b" boundary assertions and that order | ||||||||||||||||||||||||||||
| 1492 | # is extremely important (so as to match '>=' before '>', for example) | ||||||||||||||||||||||||||||
| 1493 | 54 | my $compkeys = join('|', qw( \bin\b <= >= < > =~ !~ != == = )); | |||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||
| 1495 | # a hash table of boolean operators and associated functions | ||||||||||||||||||||||||||||
| 1496 | my $boolean = { | ||||||||||||||||||||||||||||
| 1497 | 2 | ▶ | '&&' => sub { $_[0] && $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1498 | 0 | ▶ | '||' => sub { $_[0] || $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1499 | 0 | ▶ | '^' => sub { $_[0] ^ $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 1500 | 4 | ▶ | 'and' => sub { $_[0] and $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1501 | 0 | ▶ | 'or' => sub { $_[0] or $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1502 | 0 | ▶ | 'xor' => sub { $_[0] xor $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/4
$_[0] ^^ $_[1]
MC/DC0%0/2
$_[0] ^^ $_[1]
$_[0]
$_[1]
Truth table0%0/4
A ^^ B
A= $_[0]B= $_[1]
| |||||||||||||||||||||||||||||
| 1503 | 54 | }; | |||||||||||||||||||||||||||
| 1504 | my $boolkeys = join('|', | ||||||||||||||||||||||||||||
| 1505 | 54 | ▶ | map { /^\w+$/ ? "\\b$_\\b" : "\Q$_" } keys %$boolean); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1506 | |||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||
| 1508 | # DEBUG code | ||||||||||||||||||||||||||||
| 1509 | 54 | $self->_DEBUG(DBGFUNC, "_evaluate($expr, $tags)\n"); | |||||||||||||||||||||||||||
| 1510 | 54 | foreach (keys %$tags) { | |||||||||||||||||||||||||||
| 1511 | $self->_DEBUG(DBGEVAL | DBGDATA, " eval: %-10s -> %s\n", | ||||||||||||||||||||||||||||
| 1512 | 213 | $_, $tags->{ $_ }); | |||||||||||||||||||||||||||
| 1513 | } | ||||||||||||||||||||||||||||
| 1514 | |||||||||||||||||||||||||||||
| 1515 | |||||||||||||||||||||||||||||
| 1516 | # trounce leading and trailing whitespace | ||||||||||||||||||||||||||||
| 1517 | 54 | foreach ($expr) { | |||||||||||||||||||||||||||
| 1518 | 54 | s/^\s+//; | |||||||||||||||||||||||||||
| 1519 | 54 | s/\s+$//g; | |||||||||||||||||||||||||||
| 1520 | } | ||||||||||||||||||||||||||||
| 1521 | |||||||||||||||||||||||||||||
| 1522 | 54 | $self->_DEBUG(DBGEVAL, "EVAL: expr: [$expr]\n"); | |||||||||||||||||||||||||||
| 1523 | |||||||||||||||||||||||||||||
| 1524 | # throw back expressions already fully simplified; note that we evaluate | ||||||||||||||||||||||||||||
| 1525 | # expressions as strings to avoid implicit true/false evaluation | ||||||||||||||||||||||||||||
| 1526 | 54 | ▶ | if ($expr eq '1' or $expr eq '0') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$expr eq 1 or $expr eq 0
MC/DC100%2/2
$expr eq 1 or $expr eq 0
$expr eq 1
$expr eq 0
Truth table100%3/3
A or B
A= $expr eq 1B= $expr eq 0
| |||||||||||||||||||||||||||||
| 1527 | 16 | $self->_DEBUG(DBGEVAL, "EVAL: fully simplified: $expr\n"); | |||||||||||||||||||||||||||
| 1528 | 16 | return $expr; | |||||||||||||||||||||||||||
| 1529 | } | ||||||||||||||||||||||||||||
| 1530 | |||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||
| 1532 | # | ||||||||||||||||||||||||||||
| 1533 | # fully expand all expressions in parenthesis | ||||||||||||||||||||||||||||
| 1534 | # | ||||||||||||||||||||||||||||
| 1535 | |||||||||||||||||||||||||||||
| 1536 | 38 | ▶ | while ($expr =~ /(.*?)\(([^\(\)]+)\)(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1537 | 0 | $lhs = $1; | |||||||||||||||||||||||||||
| 1538 | 0 | $sub = $2; | |||||||||||||||||||||||||||
| 1539 | 0 | $rhs = $3; | |||||||||||||||||||||||||||
| 1540 | |||||||||||||||||||||||||||||
| 1541 | # parse the parenthesised expression | ||||||||||||||||||||||||||||
| 1542 | 0 | ▶ | return undef unless defined($sub = $self->_evaluate($sub, $tags)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1543 | |||||||||||||||||||||||||||||
| 1544 | # build a new expression | ||||||||||||||||||||||||||||
| 1545 | 0 | $expr = "$lhs $sub $rhs"; | |||||||||||||||||||||||||||
| 1546 | } | ||||||||||||||||||||||||||||
| 1547 | |||||||||||||||||||||||||||||
| 1548 | # check there aren't any hanging parenthesis | ||||||||||||||||||||||||||||
| 1549 | 38 | ▶ | $expr =~ /[\(\)]/ && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1550 | 0 | $self->_warn("Unmatched parenthesis: $expr\n"); | |||||||||||||||||||||||||||
| 1551 | 0 | return undef; | |||||||||||||||||||||||||||
| 1552 | }; | ||||||||||||||||||||||||||||
| 1553 | |||||||||||||||||||||||||||||
| 1554 | |||||||||||||||||||||||||||||
| 1555 | # | ||||||||||||||||||||||||||||
| 1556 | # divide expression by the first boolean operator | ||||||||||||||||||||||||||||
| 1557 | # | ||||||||||||||||||||||||||||
| 1558 | |||||||||||||||||||||||||||||
| 1559 | 38 | ▶ | if ($expr =~ /(.*?)\s*($boolkeys)\s*(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1560 | |||||||||||||||||||||||||||||
| 1561 | 6 | $lhs = $1; | |||||||||||||||||||||||||||
| 1562 | 6 | $op = $2; | |||||||||||||||||||||||||||
| 1563 | 6 | $rhs = $3; | |||||||||||||||||||||||||||
| 1564 | |||||||||||||||||||||||||||||
| 1565 | 6 | $self->_DEBUG(DBGEVAL, "EVAL: boolean split: [$lhs] [$op] [$rhs]\n"); | |||||||||||||||||||||||||||
| 1566 | |||||||||||||||||||||||||||||
| 1567 | # evaluate expression using relevant operator | ||||||||||||||||||||||||||||
| 1568 | 6 | ▶ | $result = &{ $boolean->{ $op } }( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1569 | $lhs = $self->_evaluate($lhs, $tags), | ||||||||||||||||||||||||||||
| 1570 | $rhs = $self->_evaluate($rhs, $tags) | ||||||||||||||||||||||||||||
| 1571 | ) ? 1 : 0; | ||||||||||||||||||||||||||||
| 1572 | |||||||||||||||||||||||||||||
| 1573 | 6 | $self->_DEBUG(DBGEVAL, | |||||||||||||||||||||||||||
| 1574 | "EVAL: bool: [$original] => [$lhs] [$op] [$rhs] = $result\n"); | ||||||||||||||||||||||||||||
| 1575 | 6 | return $result; | |||||||||||||||||||||||||||
| 1576 | } | ||||||||||||||||||||||||||||
| 1577 | |||||||||||||||||||||||||||||
| 1578 | |||||||||||||||||||||||||||||
| 1579 | # | ||||||||||||||||||||||||||||
| 1580 | # divide expression by the first comparitor | ||||||||||||||||||||||||||||
| 1581 | # | ||||||||||||||||||||||||||||
| 1582 | |||||||||||||||||||||||||||||
| 1583 | 32 | $lhs = $expr; | |||||||||||||||||||||||||||
| 1584 | 32 | $rhs = $op = ''; | |||||||||||||||||||||||||||
| 1585 | |||||||||||||||||||||||||||||
| 1586 | 32 | ▶ | if ($expr =~ /^\s*(.*?)\s*($compkeys)\s*(.*?)\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1587 | 28 | $lhs = $1; | |||||||||||||||||||||||||||
| 1588 | 28 | $op = $2; | |||||||||||||||||||||||||||
| 1589 | 28 | $rhs = $3; | |||||||||||||||||||||||||||
| 1590 | |||||||||||||||||||||||||||||
| 1591 | 28 | $self->_DEBUG(DBGEVAL, "EVAL: compare: [$lhs] [$op] [$rhs]\n"); | |||||||||||||||||||||||||||
| 1592 | } | ||||||||||||||||||||||||||||
| 1593 | |||||||||||||||||||||||||||||
| 1594 | # | ||||||||||||||||||||||||||||
| 1595 | # cleanup, rationalise and/or evaluate left-hand side | ||||||||||||||||||||||||||||
| 1596 | # | ||||||||||||||||||||||||||||
| 1597 | |||||||||||||||||||||||||||||
| 1598 | # left hand side is automatically dereferenced so remove any explicit | ||||||||||||||||||||||||||||
| 1599 | # dereferencing '$' character at the start | ||||||||||||||||||||||||||||
| 1600 | 32 | $lhs =~ s/^\$//; | |||||||||||||||||||||||||||
| 1601 | |||||||||||||||||||||||||||||
| 1602 | # convert lhs to lower case unless CASE sensitive | ||||||||||||||||||||||||||||
| 1603 | 32 | ▶ | $lhs = lc $lhs unless $self->{ CASE }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1604 | |||||||||||||||||||||||||||||
| 1605 | $self->_DEBUG(DBGEVAL, "EVAL: expand lhs: \$$lhs => %s\n", | ||||||||||||||||||||||||||||
| 1606 | 32 | ▶ | $tags->{ $lhs } || "<undef>"); | ||||||||||||||||||||||||||
|
Condition100%2/2
$tags->{$lhs} || '<undef>'
MC/DC100%1/1
$tags->{$lhs} || '<undef>'
$tags->{$lhs}
Truth table100%2/2
A
A= $tags->{$lhs}
| |||||||||||||||||||||||||||||
| 1607 | |||||||||||||||||||||||||||||
| 1608 | # dereference the lhs variable | ||||||||||||||||||||||||||||
| 1609 | 32 | ▶ | $lhs = $tags->{ $lhs } || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$tags->{$lhs} || 0
MC/DC100%1/1
$tags->{$lhs} || 0
$tags->{$lhs}
Truth table100%2/2
A
A= $tags->{$lhs}
| |||||||||||||||||||||||||||||
| 1610 | |||||||||||||||||||||||||||||
| 1611 | |||||||||||||||||||||||||||||
| 1612 | # | ||||||||||||||||||||||||||||
| 1613 | # no comparitor implies lhs is a simple true/false evaluated variable | ||||||||||||||||||||||||||||
| 1614 | # | ||||||||||||||||||||||||||||
| 1615 | |||||||||||||||||||||||||||||
| 1616 | 32 | ▶ | unless ($op) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1617 | 4 | ▶ | $self->_DEBUG(DBGEVAL, "EVAL: simple: [$lhs] = %s\n", $lhs ? 1 : 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1618 | 4 | ▶ | return $lhs ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1619 | } | ||||||||||||||||||||||||||||
| 1620 | |||||||||||||||||||||||||||||
| 1621 | |||||||||||||||||||||||||||||
| 1622 | # | ||||||||||||||||||||||||||||
| 1623 | # de-reference RHS of the equation ($comp) if it starts with a '$' | ||||||||||||||||||||||||||||
| 1624 | # | ||||||||||||||||||||||||||||
| 1625 | |||||||||||||||||||||||||||||
| 1626 | 28 | ▶ | if ($rhs =~ s/^\$(.*)/$1/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1627 | |||||||||||||||||||||||||||||
| 1628 | # convert variable name to lower case unless CASE sensitive | ||||||||||||||||||||||||||||
| 1629 | 4 | ▶ | $rhs = lc $rhs unless $self->{ CASE }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1630 | |||||||||||||||||||||||||||||
| 1631 | $self->_DEBUG(DBGEVAL, "EVAL: expand rhs: $rhs => %s\n", | ||||||||||||||||||||||||||||
| 1632 | 4 | ▶ | $tags->{ $rhs } || "<undef>"); | ||||||||||||||||||||||||||
|
Condition50%1/2
$tags->{$rhs} || '<undef>'
MC/DC0%0/1
$tags->{$rhs} || '<undef>'
$tags->{$rhs}
Truth table50%1/2
A
A= $tags->{$rhs}
| |||||||||||||||||||||||||||||
| 1633 | |||||||||||||||||||||||||||||
| 1634 | # de-reference variables | ||||||||||||||||||||||||||||
| 1635 | 4 | ▶ | $rhs = $tags->{ $rhs } || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$tags->{$rhs} || 0
MC/DC0%0/1
$tags->{$rhs} || 0
$tags->{$rhs}
Truth table50%1/2
A
A= $tags->{$rhs}
| |||||||||||||||||||||||||||||
| 1636 | } | ||||||||||||||||||||||||||||
| 1637 | else { | ||||||||||||||||||||||||||||
| 1638 | 24 | $self->_DEBUG(DBGEVAL, "EVAL: rhs: [$rhs]\n"); | |||||||||||||||||||||||||||
| 1639 | } | ||||||||||||||||||||||||||||
| 1640 | |||||||||||||||||||||||||||||
| 1641 | # remove surrounding quotes from rhs value | ||||||||||||||||||||||||||||
| 1642 | 28 | foreach ($rhs) { | |||||||||||||||||||||||||||
| 1643 | 28 | s/^["']//; | |||||||||||||||||||||||||||
| 1644 | 28 | s/["']$//; | |||||||||||||||||||||||||||
| 1645 | } | ||||||||||||||||||||||||||||
| 1646 | |||||||||||||||||||||||||||||
| 1647 | # force both LHS and RHS to lower case unless CASE sensitive | ||||||||||||||||||||||||||||
| 1648 | 28 | ▶ | unless ($self->{ CASE }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1649 | 28 | $lhs = lc $lhs; | |||||||||||||||||||||||||||
| 1650 | 28 | $rhs = lc $rhs; | |||||||||||||||||||||||||||
| 1651 | } | ||||||||||||||||||||||||||||
| 1652 | |||||||||||||||||||||||||||||
| 1653 | |||||||||||||||||||||||||||||
| 1654 | # | ||||||||||||||||||||||||||||
| 1655 | # evaluate the comparison statement | ||||||||||||||||||||||||||||
| 1656 | # | ||||||||||||||||||||||||||||
| 1657 | |||||||||||||||||||||||||||||
| 1658 | 28 | ▶ | $result = &{ $compare->{"\L$op"} }($lhs, $rhs) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1659 | |||||||||||||||||||||||||||||
| 1660 | 28 | $self->_DEBUG(DBGEVAL, "EVAL: comp: [%s] => [%s] [%s] [%s] = %s\n", | |||||||||||||||||||||||||||
| 1661 | $original, $lhs, $op, $rhs, $result); | ||||||||||||||||||||||||||||
| 1662 | |||||||||||||||||||||||||||||
| 1663 | 28 | $result; | |||||||||||||||||||||||||||
| 1664 | } | ||||||||||||||||||||||||||||
| 1665 | |||||||||||||||||||||||||||||
| 1666 | |||||||||||||||||||||||||||||
| 1667 | |||||||||||||||||||||||||||||
| 1668 | #======================================================================== | ||||||||||||||||||||||||||||
| 1669 | # | ||||||||||||||||||||||||||||
| 1670 | # _post_process($directive, $string) | ||||||||||||||||||||||||||||
| 1671 | # | ||||||||||||||||||||||||||||
| 1672 | # This function is called to post-process the output generated when | ||||||||||||||||||||||||||||
| 1673 | # process() conducts a SUBST or an INCLUDE operation. The FILTER and | ||||||||||||||||||||||||||||
| 1674 | # FORMAT parameters of the directive, $directive, are used to indicate | ||||||||||||||||||||||||||||
| 1675 | # the type of post-processing required. | ||||||||||||||||||||||||||||
| 1676 | # | ||||||||||||||||||||||||||||
| 1677 | # Returns the processed string. | ||||||||||||||||||||||||||||
| 1678 | # | ||||||||||||||||||||||||||||
| 1679 | #======================================================================== | ||||||||||||||||||||||||||||
| 1680 | |||||||||||||||||||||||||||||
| 1681 | 12 | ▶ | sub _post_process { | ||||||||||||||||||||||||||
|
Subroutine
_post_process: called CC=20 SCAR=32.4
| |||||||||||||||||||||||||||||
| 1682 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 1683 | 12 | my $directive = shift; | |||||||||||||||||||||||||||
| 1684 | 12 | my $line = shift; | |||||||||||||||||||||||||||
| 1685 | 12 | my $formats = { | |||||||||||||||||||||||||||
| 1686 | QUOTED => '"%s"', | ||||||||||||||||||||||||||||
| 1687 | DQUOTED => '"%s"', | ||||||||||||||||||||||||||||
| 1688 | SQUOTED => "'%s'", | ||||||||||||||||||||||||||||
| 1689 | MONEY => "%P%.2f", # '%P' says "use printf() not time2str()" | ||||||||||||||||||||||||||||
| 1690 | }; | ||||||||||||||||||||||||||||
| 1691 | 12 | my ($pre, $post); | |||||||||||||||||||||||||||
| 1692 | 12 | my @lines; | |||||||||||||||||||||||||||
| 1693 | |||||||||||||||||||||||||||||
| 1694 | |||||||||||||||||||||||||||||
| 1695 | # DEBUG code | ||||||||||||||||||||||||||||
| 1696 | 12 | ▶ | if ($self->{ DEBUGLEVEL } & DBGFUNC) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1697 | 0 | my $dbgline = $line; | |||||||||||||||||||||||||||
| 1698 | 0 | $dbgline =~ s/\n/\\n/g; | |||||||||||||||||||||||||||
| 1699 | 0 | $dbgline =~ s/\t/\\t/g; | |||||||||||||||||||||||||||
| 1700 | 0 | ▶ | substr($dbgline, 0, 16) = "..." | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1701 | if length $dbgline > 16; | ||||||||||||||||||||||||||||
| 1702 | 0 | $dbgline = "\"$dbgline\""; | |||||||||||||||||||||||||||
| 1703 | 0 | $self->_DEBUG(DBGFUNC, "_post_process($directive, $dbgline)\n"); | |||||||||||||||||||||||||||
| 1704 | } | ||||||||||||||||||||||||||||
| 1705 | 12 | $self->_DEBUG(DBGPOST, "Post-process: \n[$line]\n"); | |||||||||||||||||||||||||||
| 1706 | |||||||||||||||||||||||||||||
| 1707 | |||||||||||||||||||||||||||||
| 1708 | # no need to do anything if there's nothing to operate on | ||||||||||||||||||||||||||||
| 1709 | 12 | ▶ | return "" unless defined $line && length $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $line and length $line
MC/DC0%0/2
defined $line and length $line
defined $line
length $line
Truth table33%1/3
A and B
A= defined $lineB= length $line
| |||||||||||||||||||||||||||||
| 1710 | |||||||||||||||||||||||||||||
| 1711 | # split into lines, accounting for a trailing newline which would | ||||||||||||||||||||||||||||
| 1712 | # otherwise be ignored by split() | ||||||||||||||||||||||||||||
| 1713 | 12 | @lines = split(/\n/, $line); | |||||||||||||||||||||||||||
| 1714 | 12 | ▶ | push(@lines, "") if chomp($line); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1715 | |||||||||||||||||||||||||||||
| 1716 | |||||||||||||||||||||||||||||
| 1717 | 12 | $self->_DEBUG(DBGPOST, " -> [%s]\n" , join("]\n [", @lines)); | |||||||||||||||||||||||||||
| 1718 | |||||||||||||||||||||||||||||
| 1719 | |||||||||||||||||||||||||||||
| 1720 | # see if the "FILTER" option is specified | ||||||||||||||||||||||||||||
| 1721 | 12 | ▶ | if (defined($directive->{ FILTER })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1722 | |||||||||||||||||||||||||||||
| 1723 | # extract the filter name and parameters: <name>(<params>) | ||||||||||||||||||||||||||||
| 1724 | 4 | $directive->{ FILTER } =~ /([^(]+)(?:\((.*)\))?/; | |||||||||||||||||||||||||||
| 1725 | 4 | my $fltname = $1; | |||||||||||||||||||||||||||
| 1726 | |||||||||||||||||||||||||||||
| 1727 | # split filter parameters and remove enclosing quotes | ||||||||||||||||||||||||||||
| 1728 | 4 | ▶ | my @fltparams = split(/\s*,\s*/, $2 || ""); | ||||||||||||||||||||||||||
|
Condition50%1/2
$2 || ''
MC/DC0%0/1
$2 || ''
$2
Truth table50%1/2
A
A= $2
| |||||||||||||||||||||||||||||
| 1729 | 4 | foreach (@fltparams) { | |||||||||||||||||||||||||||
| 1730 | 5 | s/^"//; | |||||||||||||||||||||||||||
| 1731 | 5 | s/"$//; | |||||||||||||||||||||||||||
| 1732 | } | ||||||||||||||||||||||||||||
| 1733 | |||||||||||||||||||||||||||||
| 1734 | |||||||||||||||||||||||||||||
| 1735 | # is there a filter function with the name specified? | ||||||||||||||||||||||||||||
| 1736 | 4 | ▶ | if (ref($self->{ FILTER }->{ $fltname }) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1737 | |||||||||||||||||||||||||||||
| 1738 | 4 | $self->_DEBUG(DBGINFO, "filter: $fltname(%s)\n", | |||||||||||||||||||||||||||
| 1739 | join(", ", $fltname, @fltparams)); | ||||||||||||||||||||||||||||
| 1740 | |||||||||||||||||||||||||||||
| 1741 | # deref filter code to speed up multi-line processing | ||||||||||||||||||||||||||||
| 1742 | 4 | my $fltfn = $self->{ FILTER }->{ $fltname }; | |||||||||||||||||||||||||||
| 1743 | |||||||||||||||||||||||||||||
| 1744 | # feed each line through filter function | ||||||||||||||||||||||||||||
| 1745 | 4 | foreach (@lines) { | |||||||||||||||||||||||||||
| 1746 | 8 | $pre = $_; | |||||||||||||||||||||||||||
| 1747 | 8 | $_ = &$fltfn($fltname, $_, @fltparams); | |||||||||||||||||||||||||||
| 1748 | 8 | $post = $_; | |||||||||||||||||||||||||||
| 1749 | |||||||||||||||||||||||||||||
| 1750 | 8 | ▶ | if ($self->{ DEBUGLEVEL } & DBGPOST) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1751 | 0 | $self->_DEBUG(DBGDATA, | |||||||||||||||||||||||||||
| 1752 | "filter: [ $pre ]\n -> [ $post ]\n"); | ||||||||||||||||||||||||||||
| 1753 | } | ||||||||||||||||||||||||||||
| 1754 | } | ||||||||||||||||||||||||||||
| 1755 | } | ||||||||||||||||||||||||||||
| 1756 | else { | ||||||||||||||||||||||||||||
| 1757 | 0 | $self->_warn("$fltname: non-existant or invalid filter\n"); | |||||||||||||||||||||||||||
| 1758 | } | ||||||||||||||||||||||||||||
| 1759 | } | ||||||||||||||||||||||||||||
| 1760 | |||||||||||||||||||||||||||||
| 1761 | |||||||||||||||||||||||||||||
| 1762 | # | ||||||||||||||||||||||||||||
| 1763 | # if the "format=<template>" option is specified, the output | ||||||||||||||||||||||||||||
| 1764 | # is formatted in one of two ways. If the format string contains | ||||||||||||||||||||||||||||
| 1765 | # a sequence matching the pattern "%[^s]" (i.e. any %<character> | ||||||||||||||||||||||||||||
| 1766 | # marker other than '%s'), it is assumed to be a date and is | ||||||||||||||||||||||||||||
| 1767 | # processed using time2str() from Date::Format. | ||||||||||||||||||||||||||||
| 1768 | # | ||||||||||||||||||||||||||||
| 1769 | # If the format string contains no other percent marker than | ||||||||||||||||||||||||||||
| 1770 | # "%s", it is assumed to be a printf()-like format and is treated | ||||||||||||||||||||||||||||
| 1771 | # appropriately. Luckily enough, "%s" produces the same output | ||||||||||||||||||||||||||||
| 1772 | # from both printf() and time2str() functions ("%s" denotes number | ||||||||||||||||||||||||||||
| 1773 | # of seconds since the epoch - the same value stored in the string | ||||||||||||||||||||||||||||
| 1774 | # and interpolated as such by perl when doing sprintf("%s", $str)). | ||||||||||||||||||||||||||||
| 1775 | # | ||||||||||||||||||||||||||||
| 1776 | # To explicitly indicate a printf()-like format string, the marker | ||||||||||||||||||||||||||||
| 1777 | # "%P" can be embedded anywhere in the string. This is then | ||||||||||||||||||||||||||||
| 1778 | # ignored in the format process. e.g. "%P%4.2f", 12.3 => "12.30" | ||||||||||||||||||||||||||||
| 1779 | # | ||||||||||||||||||||||||||||
| 1780 | 12 | ▶ | if (defined($directive->{ FORMAT })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1781 | 9 | my $format = $directive->{ FORMAT }; | |||||||||||||||||||||||||||
| 1782 | |||||||||||||||||||||||||||||
| 1783 | # the format may refer to a pre-defined one which is to be used | ||||||||||||||||||||||||||||
| 1784 | # in its place | ||||||||||||||||||||||||||||
| 1785 | $format = $formats->{ uc $format } | ||||||||||||||||||||||||||||
| 1786 | 9 | ▶ | if ($format !~ /\W/ && defined $formats->{ uc $format }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $format =~ /\W/ and defined $$formats{uc $format}
MC/DC50%1/2
not $format =~ /\W/ and defined $$formats{uc $format}
not $format =~ /\W/
defined $$formats{uc $format}
Truth table66%2/3
not(A) and B
A= not $format =~ /\W/B= defined $$formats{uc $format}
| |||||||||||||||||||||||||||||
| 1787 | |||||||||||||||||||||||||||||
| 1788 | 9 | my $fmtdate = ($format =~ /%[^s]/); # use time2str()? | |||||||||||||||||||||||||||
| 1789 | |||||||||||||||||||||||||||||
| 1790 | # does the format include '%P' to request printf()? | ||||||||||||||||||||||||||||
| 1791 | 9 | ▶ | $fmtdate = 0 if ($fmtdate && ($format =~ s/%P//g)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fmtdate and $format =~ s/%P//g
MC/DC100%2/2
$fmtdate and $format =~ s/%P//g
$fmtdate
$format =~ s/%P//g
Truth table100%3/3
A and B
A= $fmtdateB= $format =~ s/%P//g
| |||||||||||||||||||||||||||||
| 1792 | |||||||||||||||||||||||||||||
| 1793 | 9 | my $safefmt; # protect '%s' from printf in _DEBUG() | |||||||||||||||||||||||||||
| 1794 | 9 | ($safefmt = $format) =~ s/%/%%/g; | |||||||||||||||||||||||||||
| 1795 | |||||||||||||||||||||||||||||
| 1796 | 9 | $self->_DEBUG(DBGPOST, "format: $safefmt\n"); | |||||||||||||||||||||||||||
| 1797 | |||||||||||||||||||||||||||||
| 1798 | # unescape quotes, newlines and tabs | ||||||||||||||||||||||||||||
| 1799 | 9 | $format =~ s/\\"/"/g; | |||||||||||||||||||||||||||
| 1800 | 9 | $format =~ s/\\n/\n/g; | |||||||||||||||||||||||||||
| 1801 | 9 | $format =~ s/\\t/\t/g; | |||||||||||||||||||||||||||
| 1802 | |||||||||||||||||||||||||||||
| 1803 | 9 | foreach (@lines) { | |||||||||||||||||||||||||||
| 1804 | 12 | $pre = $_; | |||||||||||||||||||||||||||
| 1805 | 12 | ▶ | $_ = $fmtdate | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1806 | ? time2str($format, $_) | ||||||||||||||||||||||||||||
| 1807 | : sprintf($format, $_); | ||||||||||||||||||||||||||||
| 1808 | 12 | $post = $_; | |||||||||||||||||||||||||||
| 1809 | |||||||||||||||||||||||||||||
| 1810 | 12 | ▶ | if ($self->{ DEBUGLEVEL } & DBGPOST) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1811 | 0 | $self->_DEBUG(DBGDATA, | |||||||||||||||||||||||||||
| 1812 | "format: [ $pre ]\n -> [ $post ]\n"); | ||||||||||||||||||||||||||||
| 1813 | } | ||||||||||||||||||||||||||||
| 1814 | } | ||||||||||||||||||||||||||||
| 1815 | } | ||||||||||||||||||||||||||||
| 1816 | |||||||||||||||||||||||||||||
| 1817 | # reconstruct all lines back into a single string | ||||||||||||||||||||||||||||
| 1818 | 12 | join("\n", @lines); | |||||||||||||||||||||||||||
| 1819 | } | ||||||||||||||||||||||||||||
| 1820 | |||||||||||||||||||||||||||||
| 1821 | |||||||||||||||||||||||||||||
| 1822 | |||||||||||||||||||||||||||||
| 1823 | #======================================================================== | ||||||||||||||||||||||||||||
| 1824 | # | ||||||||||||||||||||||||||||
| 1825 | # _dump_symbol($symbol) | ||||||||||||||||||||||||||||
| 1826 | # | ||||||||||||||||||||||||||||
| 1827 | # Dumps the contents of the symbol table entry indexed by $symbol using | ||||||||||||||||||||||||||||
| 1828 | # the _DEBUG function. The output is processed to be easily readable. | ||||||||||||||||||||||||||||
| 1829 | # | ||||||||||||||||||||||||||||
| 1830 | #======================================================================== | ||||||||||||||||||||||||||||
| 1831 | |||||||||||||||||||||||||||||
| 1832 | 0 | ▶ | sub _dump_symbol { | ||||||||||||||||||||||||||
|
Subroutine
_dump_symbol: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 1833 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 1834 | 0 | my $symbol = shift; | |||||||||||||||||||||||||||
| 1835 | 0 | my ($factory, $directive); | |||||||||||||||||||||||||||
| 1836 | 0 | my $copy; | |||||||||||||||||||||||||||
| 1837 | |||||||||||||||||||||||||||||
| 1838 | |||||||||||||||||||||||||||||
| 1839 | 0 | $self->_DEBUG(DBGCONT, "-- Pre-processed symbol: $symbol %s\n", | |||||||||||||||||||||||||||
| 1840 | '-' x (72 - 26 - length($symbol))); | ||||||||||||||||||||||||||||
| 1841 | |||||||||||||||||||||||||||||
| 1842 | # get a reference to the factory object and call directive_type() | ||||||||||||||||||||||||||||
| 1843 | # to determine the kind of Directive objects it creates | ||||||||||||||||||||||||||||
| 1844 | 0 | ▶ | return unless $factory = $self->_factory(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1845 | 0 | $directive = $factory->directive_type(); | |||||||||||||||||||||||||||
| 1846 | |||||||||||||||||||||||||||||
| 1847 | 0 | foreach (@{ $self->{ SYMTABLE }->{ $symbol } }) { | |||||||||||||||||||||||||||
| 1848 | |||||||||||||||||||||||||||||
| 1849 | # is this a directive? | ||||||||||||||||||||||||||||
| 1850 | 0 | ▶ | ref($_) eq $directive && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1851 | $self->_DEBUG(DBGCONT, "%s %s %s %s\n", | ||||||||||||||||||||||||||||
| 1852 | $self->{ MAGIC }->[0], | ||||||||||||||||||||||||||||
| 1853 | $_->{ TYPE }, | ||||||||||||||||||||||||||||
| 1854 | $_->{ IDENTIFIER } || "<none>", | ||||||||||||||||||||||||||||
| 1855 | 0 | ▶ | $self->{ MAGIC }->[1]); | ||||||||||||||||||||||||||
|
Condition0%0/2
$_->{'IDENTIFIER'} || '<none>'
MC/DC0%0/1
$_->{'IDENTIFIER'} || '<none>'
$_->{'IDENTIFIER'}
Truth table0%0/2
A
A= $_->{'IDENTIFIER'}
| |||||||||||||||||||||||||||||
| 1856 | 0 | next; | |||||||||||||||||||||||||||
| 1857 | }; | ||||||||||||||||||||||||||||
| 1858 | |||||||||||||||||||||||||||||
| 1859 | # take a copy of the line and convert CR to visible \\n's | ||||||||||||||||||||||||||||
| 1860 | 0 | ($copy = $_) =~ s/\n/\\n/gm; | |||||||||||||||||||||||||||
| 1861 | |||||||||||||||||||||||||||||
| 1862 | 0 | map { $self->_DEBUG(DBGCONT, "[ $_ ]\n"); } split(/\n/, $copy); | |||||||||||||||||||||||||||
| 1863 | } | ||||||||||||||||||||||||||||
| 1864 | |||||||||||||||||||||||||||||
| 1865 | 0 | $self->_DEBUG(DBGCONT, "%s\n", '-' x 72); | |||||||||||||||||||||||||||
| 1866 | } | ||||||||||||||||||||||||||||
| 1867 | |||||||||||||||||||||||||||||
| 1868 | |||||||||||||||||||||||||||||
| 1869 | |||||||||||||||||||||||||||||
| 1870 | #======================================================================== | ||||||||||||||||||||||||||||
| 1871 | # | ||||||||||||||||||||||||||||
| 1872 | # _warn(@_) | ||||||||||||||||||||||||||||
| 1873 | # | ||||||||||||||||||||||||||||
| 1874 | # Prints the specified warning message(s) using the warning function | ||||||||||||||||||||||||||||
| 1875 | # specified in $self->{ ERRORFN } or "print STDERR", if undefined. | ||||||||||||||||||||||||||||
| 1876 | # | ||||||||||||||||||||||||||||
| 1877 | #======================================================================== | ||||||||||||||||||||||||||||
| 1878 | |||||||||||||||||||||||||||||
| 1879 | 2 | ▶ | sub _warn { | ||||||||||||||||||||||||||
|
Subroutine
_warn: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 1880 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 1881 | |||||||||||||||||||||||||||||
| 1882 | 2 | ▶ | return &{ $self->{ ERRORFN } }(@_) if defined($self->{ ERRORFN }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1883 | |||||||||||||||||||||||||||||
| 1884 | 0 | print STDERR @_, "\n"; | |||||||||||||||||||||||||||
| 1885 | } | ||||||||||||||||||||||||||||
| 1886 | |||||||||||||||||||||||||||||
| 1887 | |||||||||||||||||||||||||||||
| 1888 | |||||||||||||||||||||||||||||
| 1889 | #======================================================================== | ||||||||||||||||||||||||||||
| 1890 | # | ||||||||||||||||||||||||||||
| 1891 | # _error($message) | ||||||||||||||||||||||||||||
| 1892 | # | ||||||||||||||||||||||||||||
| 1893 | # Private error reporting method. Sets internal ERROR value (which can | ||||||||||||||||||||||||||||
| 1894 | # be retrieved using the public method error(), and calls | ||||||||||||||||||||||||||||
| 1895 | # $self->_warn($message) to report the error. | ||||||||||||||||||||||||||||
| 1896 | # | ||||||||||||||||||||||||||||
| 1897 | #======================================================================== | ||||||||||||||||||||||||||||
| 1898 | |||||||||||||||||||||||||||||
| 1899 | 0 | ▶ | sub _error { | ||||||||||||||||||||||||||
|
Subroutine
_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 1900 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 1901 | 0 | ▶ | my $message = shift || ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 1902 | |||||||||||||||||||||||||||||
| 1903 | 0 | $self->{ ERROR } = $message; | |||||||||||||||||||||||||||
| 1904 | 0 | $self->_warn($message); | |||||||||||||||||||||||||||
| 1905 | } | ||||||||||||||||||||||||||||
| 1906 | |||||||||||||||||||||||||||||
| 1907 | |||||||||||||||||||||||||||||
| 1908 | |||||||||||||||||||||||||||||
| 1909 | #======================================================================== | ||||||||||||||||||||||||||||
| 1910 | # | ||||||||||||||||||||||||||||
| 1911 | # _parse_error($message) | ||||||||||||||||||||||||||||
| 1912 | # | ||||||||||||||||||||||||||||
| 1913 | # Private error reporting method used by the parser. Add an additional | ||||||||||||||||||||||||||||
| 1914 | # file/line report to the error message. | ||||||||||||||||||||||||||||
| 1915 | # | ||||||||||||||||||||||||||||
| 1916 | #======================================================================== | ||||||||||||||||||||||||||||
| 1917 | |||||||||||||||||||||||||||||
| 1918 | 0 | ▶ | sub _parse_error { | ||||||||||||||||||||||||||
|
Subroutine
_parse_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 1919 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 1920 | 0 | ▶ | my $message = shift || ""; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 1921 | |||||||||||||||||||||||||||||
| 1922 | $self->_error( | ||||||||||||||||||||||||||||
| 1923 | sprintf("Parse error at %s line %s:\n $message", | ||||||||||||||||||||||||||||
| 1924 | $self->{ INPUT }, $self->{ LINENO }) | ||||||||||||||||||||||||||||
| 1925 | 0 | ); | |||||||||||||||||||||||||||
| 1926 | } | ||||||||||||||||||||||||||||
| 1927 | |||||||||||||||||||||||||||||
| 1928 | |||||||||||||||||||||||||||||
| 1929 | |||||||||||||||||||||||||||||
| 1930 | #======================================================================== | ||||||||||||||||||||||||||||
| 1931 | # | ||||||||||||||||||||||||||||
| 1932 | # _DEBUG($level, $message, @params) | ||||||||||||||||||||||||||||
| 1933 | # | ||||||||||||||||||||||||||||
| 1934 | # If ($self->{ DEBUGLEVEL } & $level) equate trues, the specified message | ||||||||||||||||||||||||||||
| 1935 | # is printed using the debug function defined in $self->{ DEBUGFUNC }. | ||||||||||||||||||||||||||||
| 1936 | # If no debug function is defined, the ($message, @params) are formatted | ||||||||||||||||||||||||||||
| 1937 | # as per printf(3) and printed to STDERR, prefixing each line with "D> ". | ||||||||||||||||||||||||||||
| 1938 | # | ||||||||||||||||||||||||||||
| 1939 | #======================================================================== | ||||||||||||||||||||||||||||
| 1940 | |||||||||||||||||||||||||||||
| 1941 | 3450 | ▶ | sub _DEBUG { | ||||||||||||||||||||||||||
|
Subroutine
_DEBUG: called CC=3 SCAR=16.3
| |||||||||||||||||||||||||||||
| 1942 | 3450 | my $self = shift; | |||||||||||||||||||||||||||
| 1943 | 3450 | my $level = shift; | |||||||||||||||||||||||||||
| 1944 | 3450 | my $output; | |||||||||||||||||||||||||||
| 1945 | |||||||||||||||||||||||||||||
| 1946 | 3450 | ▶ | return unless (($self->{ DEBUGLEVEL } & $level) == $level); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1947 | |||||||||||||||||||||||||||||
| 1948 | 0 | ▶ | return &{ $self->{ DEBUGFN } }(@_) if defined($self->{ DEBUGFN }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1949 | |||||||||||||||||||||||||||||
| 1950 | # sprintf expects a scalar first, so "sprintf(@_)" doesn't work | ||||||||||||||||||||||||||||
| 1951 | 0 | $output = sprintf(shift, @_); | |||||||||||||||||||||||||||
| 1952 | |||||||||||||||||||||||||||||
| 1953 | # prefix each line with "D> " and print to STDERR | ||||||||||||||||||||||||||||
| 1954 | 0 | $output =~ s/^/D> /mg; | |||||||||||||||||||||||||||
| 1955 | 0 | print STDERR $output; | |||||||||||||||||||||||||||
| 1956 | } | ||||||||||||||||||||||||||||
| 1957 | |||||||||||||||||||||||||||||
| 1958 | |||||||||||||||||||||||||||||
| 1959 | |||||||||||||||||||||||||||||
| 1960 | 9 | 1; | |||||||||||||||||||||||||||
| 1961 | __END__ | ||||||||||||||||||||||||||||