| 1 | package Text::MacroScript; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 92 | require v5.10; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 92 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 92 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 92 | ▶ | use Carp qw( carp croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 92 | our @CARP_NOT = ( __PACKAGE__ ); | |||||||||||||||||||||||||||
| 10 | 92 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 92 | ▶ | use vars qw( $VERSION $NAME_RE $COMMENT ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 92 | $VERSION = '2.14'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 92 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 92 | $NAME_RE = qr/ [^\s\[\|\]\#]+ /x; # name cannot contain blanks [ | ] # | |||||||||||||||||||||||||||
| 17 | 92 | $COMMENT = "%%"; # comment macro | |||||||||||||||||||||||||||
| 18 | }; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 21 | # object to hold current input stack for nested structs | ||||||||||||||||||||||||||||
| 22 | 92 | ▶ | use enum qw( CTX_ARGS=1 CTX_TEXT ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | package # hide this from CPAN | ||||||||||||||||||||||||||||
| 25 | Text::MacroScript::Context; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | use Object::Tiny::RW | ||||||||||||||||||||||||||||
| 28 | 92 | ▶ | 'type', # type of struct to match, one of CTX_... | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 'start_line_nr', # line number where struct started | ||||||||||||||||||||||||||||
| 30 | 'commit_func', # function to call when struct ends | ||||||||||||||||||||||||||||
| 31 | # passed $output_ref argument | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # collecting parameters | ||||||||||||||||||||||||||||
| 34 | 'args', # current collected arguments | ||||||||||||||||||||||||||||
| 35 | 'open_parens', # number of open parenthesis | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # end text collection | ||||||||||||||||||||||||||||
| 38 | 'end_text_re', # regexp to end _parse_collect_text() | ||||||||||||||||||||||||||||
| 39 | 'eat_blanks', # eat blanks after end of [] | ||||||||||||||||||||||||||||
| 40 | 92 | ; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 328 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 328 | my($class, $type, $start_line_nr, $commit_func, $end_text_re, $eat_blanks) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 328 | my $self = $class->SUPER::new( | |||||||||||||||||||||||||||
| 46 | type => $type, | ||||||||||||||||||||||||||||
| 47 | start_line_nr => $start_line_nr, | ||||||||||||||||||||||||||||
| 48 | commit_func => $commit_func, | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | args => [], | ||||||||||||||||||||||||||||
| 51 | open_parens => 1, # init at 1, as first '[' is already matched | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | end_text_re => $end_text_re, | ||||||||||||||||||||||||||||
| 54 | eat_blanks => $eat_blanks, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | 328 | return $self; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 61 | # main object | ||||||||||||||||||||||||||||
| 62 | use Object::Tiny::RW | ||||||||||||||||||||||||||||
| 63 | 92 | ▶ | 'parse_func', # current parsing function | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 'file', # current input file name for error messages | ||||||||||||||||||||||||||||
| 66 | 'line_nr', # current line number | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 'context', # stack of Text::MacroScript::Context, empty if none | ||||||||||||||||||||||||||||
| 69 | 'actions', # hash of text -> function to call if matched | ||||||||||||||||||||||||||||
| 70 | 'variables', # hash of variable name -> current value | ||||||||||||||||||||||||||||
| 71 | 'macros', # hash of scripts/macros name -> body | ||||||||||||||||||||||||||||
| 72 | 'is_script', # TRUE for script, false for macro | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 'args', # list of arguments to script | ||||||||||||||||||||||||||||
| 75 | 'regexp', # big regexp computed each time text_action changes | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 'embedded', # true if parsing embedded text | ||||||||||||||||||||||||||||
| 78 | 'in_embedded', # true if inside embedded delimiters | ||||||||||||||||||||||||||||
| 79 | 'opendelim', # open delimiter for embedded processing | ||||||||||||||||||||||||||||
| 80 | 'closedelim', # close delimiter for embedded processing | ||||||||||||||||||||||||||||
| 81 | 'comment', # True to create the %%[] comment macro | ||||||||||||||||||||||||||||
| 82 | 92 | ; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 85 | # new | ||||||||||||||||||||||||||||
| 86 | 201 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 201 | my($class, %opts) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 201 | my $self = $class->SUPER::new( | |||||||||||||||||||||||||||
| 90 | parse_func => \&_parse_execute, | ||||||||||||||||||||||||||||
| 91 | file => '-', | ||||||||||||||||||||||||||||
| 92 | line_nr => 1, | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | context => [], | ||||||||||||||||||||||||||||
| 95 | actions => {}, | ||||||||||||||||||||||||||||
| 96 | variables => {}, | ||||||||||||||||||||||||||||
| 97 | macros => {}, | ||||||||||||||||||||||||||||
| 98 | is_script => {}, | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | args => [], | ||||||||||||||||||||||||||||
| 101 | regexp => qr//, | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | embedded => 0, | ||||||||||||||||||||||||||||
| 104 | in_embedded => 0, | ||||||||||||||||||||||||||||
| 105 | opendelim => '<:', | ||||||||||||||||||||||||||||
| 106 | closedelim => ':>', | ||||||||||||||||||||||||||||
| 107 | comment => 0, | ||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||
| 109 | 201 | $self->_update_regexp; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # parse options: -comment | ||||||||||||||||||||||||||||
| 112 | 201 | ▶ | if ($opts{-comment}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 5 | $self->_define_standard_comment; | |||||||||||||||||||||||||||
| 114 | 5 | $self->comment(1); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 201 | delete $opts{-comment}; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # parse options: -embedded | ||||||||||||||||||||||||||||
| 119 | 201 | ▶ | if ($opts{-embedded} || defined($opts{-opendelim})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opts{"-embedded"} or defined $opts{"-opendelim"}
MC/DC100%2/2
$opts{"-embedded"} or defined $opts{"-opendelim"}
$opts{"-embedded"}
defined $opts{"-opendelim"}
Truth table100%3/3
A or B
A= $opts{"-embedded"}B= defined $opts{"-opendelim"}
| |||||||||||||||||||||||||||||
| 120 | 35 | $self->embedded(1); | |||||||||||||||||||||||||||
| 121 | 35 | ▶ | $self->opendelim($opts{-opendelim} // "<:"); | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"-opendelim"} // "<:"
MC/DC100%1/1
$opts{"-opendelim"} // "<:"
$opts{"-opendelim"}
Truth table100%2/2
A
A= $opts{"-opendelim"}
| |||||||||||||||||||||||||||||
| 122 | 35 | ▶ | $self->closedelim($opts{-closedelim} // $opts{-opendelim} // ":>"); | ||||||||||||||||||||||||||
|
Condition100%5/5
($opts{"-closedelim"} // $opts{"-opendelim"}) // ":>"
$opts{"-closedelim"} // $opts{"-opendelim"}
MC/DC100%2/2
($opts{"-closedelim"} // $opts{"-opendelim"}) // ":>"
$opts{"-closedelim"}
$opts{"-opendelim"}
Truth table100%3/3
A // B
A= $opts{"-closedelim"}B= $opts{"-opendelim"}
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 201 | delete @opts{qw( -embedded -opendelim -closedelim)}; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # parse options: -variable | ||||||||||||||||||||||||||||
| 127 | 201 | ▶ | if ($opts{-variable}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 6 | foreach (@{$opts{-variable}}) { | |||||||||||||||||||||||||||
| 129 | 12 | my($name, $value) = @$_; | |||||||||||||||||||||||||||
| 130 | 12 | $self->define_variable($name, $value); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 201 | delete $opts{-variable}; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # parse options: -macro | ||||||||||||||||||||||||||||
| 136 | 201 | ▶ | if ($opts{-macro}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 9 | foreach (@{$opts{-macro}}) { | |||||||||||||||||||||||||||
| 138 | 19 | my($name, $value) = @$_; | |||||||||||||||||||||||||||
| 139 | 19 | $self->define_macro($name, $value); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 201 | delete $opts{-macro}; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # parse options: -script | ||||||||||||||||||||||||||||
| 145 | 201 | ▶ | if ($opts{-script}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 10 | foreach (@{$opts{-script}}) { | |||||||||||||||||||||||||||
| 147 | 19 | my($name, $value) = @$_; | |||||||||||||||||||||||||||
| 148 | 19 | $self->define_script($name, $value); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 201 | delete $opts{-script}; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # parse options: -file | ||||||||||||||||||||||||||||
| 154 | 201 | ▶ | if ($opts{-file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 41 | foreach my $file (@{$opts{-file}}) { | |||||||||||||||||||||||||||
| 156 | 42 | $self->load_file($file); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 200 | delete $opts{-file}; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # check for invalid options | ||||||||||||||||||||||||||||
| 162 | 200 | ▶ | croak "Invalid options ".join(",", sort keys %opts) if %opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 199 | return $self; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 168 | # error | ||||||||||||||||||||||||||||
| 169 | 58 | ▶ | sub _error { | ||||||||||||||||||||||||||
|
Subroutine
_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 170 | 58 | my($self, $message) = @_; | |||||||||||||||||||||||||||
| 171 | 58 | chomp($message); | |||||||||||||||||||||||||||
| 172 | 58 | die "Error at file ", $self->file, " line ", $self->line_nr, ": ", $message, "\n"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 176 | # contexts | ||||||||||||||||||||||||||||
| 177 | 328 | ▶ | sub _push_context { | ||||||||||||||||||||||||||
|
Subroutine
_push_context: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | 328 | my($self, $type, $commit_func, $end_text_re, $eat_blanks) = @_; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 328 | my $previous_parse = $self->parse_func; | |||||||||||||||||||||||||||
| 181 | my $context = Text::MacroScript::Context->new($type, $self->line_nr, | ||||||||||||||||||||||||||||
| 182 | 314 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 183 | 314 | my($output_ref) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # pop context | ||||||||||||||||||||||||||||
| 186 | 314 | my $context = $self->_last_context_assert($type); | |||||||||||||||||||||||||||
| 187 | 314 | my @args = @{$context->args}; | |||||||||||||||||||||||||||
| 188 | 314 | $self->_pop_context; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # reset parser - it will be used when defining the variable | ||||||||||||||||||||||||||||
| 191 | 314 | $self->parse_func( $previous_parse ); | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # call commit function with input arguments | ||||||||||||||||||||||||||||
| 194 | 314 | $commit_func->($output_ref, @args); | |||||||||||||||||||||||||||
| 195 | }, | ||||||||||||||||||||||||||||
| 196 | 328 | $end_text_re, | |||||||||||||||||||||||||||
| 197 | $eat_blanks); | ||||||||||||||||||||||||||||
| 198 | 328 | push @{$self->context}, $context; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1033 | ▶ | sub _last_context { | ||||||||||||||||||||||||||
|
Subroutine
_last_context: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 202 | 1033 | my($self) = @_; | |||||||||||||||||||||||||||
| 203 | 1033 | ▶ | $self->_error("Unbalanced close structure") unless @{$self->context}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 1033 | return $self->context->[-1]; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 705 | ▶ | sub _last_context_assert { | ||||||||||||||||||||||||||
|
Subroutine
_last_context_assert: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 208 | 705 | my($self, $type) = @_; | |||||||||||||||||||||||||||
| 209 | 705 | my $context = $self->_last_context(); | |||||||||||||||||||||||||||
| 210 | 705 | ▶ | $self->_error("Unbalanced close structure") unless $type == $context->type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 705 | return $context; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 314 | ▶ | sub _pop_context { | ||||||||||||||||||||||||||
|
Subroutine
_pop_context: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 215 | 314 | my($self) = @_; | |||||||||||||||||||||||||||
| 216 | 314 | $self->_last_context(); | |||||||||||||||||||||||||||
| 217 | 314 | pop @{$self->context}; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 221 | # Destroy object, syntax error if input not complete - e.g. missing close struct | ||||||||||||||||||||||||||||
| 222 | 178 | ▶ | DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 223 | 178 | my($self) = @_; | |||||||||||||||||||||||||||
| 224 | 178 | ▶ | if (@{$self->context}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 14 | my $context = $self->_last_context; | |||||||||||||||||||||||||||
| 226 | 14 | ▶ | $self->line_nr( $context ? $context->start_line_nr : "unknown" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 14 | $self->_error("Unbalanced open structure at end of file"); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 232 | # create the parsing regexp | ||||||||||||||||||||||||||||
| 233 | 621 | ▶ | sub _update_regexp { | ||||||||||||||||||||||||||
|
Subroutine
_update_regexp: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 234 | 621 | my($self) = @_; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 92 | ▶ | use re 'eval'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 621 | my $regexp = '(?'; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # escape chars | ||||||||||||||||||||||||||||
| 241 | 621 | $regexp .= '|'.qr/ (?> \\ ( [\#\%] ) (?{ \&_match_escape }) ) /mx; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # escape newline | ||||||||||||||||||||||||||||
| 244 | 621 | $regexp .= '|'.qr/ (?> \\ \n (?{ \&_match_escape_newline }) ) /mx; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # %DEFINE_VARIABLE | ||||||||||||||||||||||||||||
| 247 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% DEFINE_VARIABLE | ||||||||||||||||||||||||||||
| 248 | 621 | (?{ \&_match_define_variable }) ) /mx; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | # %UNDEFINE_ALL_VARIABLE | ||||||||||||||||||||||||||||
| 251 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% UNDEFINE_ALL_VARIABLE \s* | ||||||||||||||||||||||||||||
| 252 | 621 | (?{ \&_match_undefine_all_variable }) ) /mx; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # %UNDEFINE_VARIABLE | ||||||||||||||||||||||||||||
| 255 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% UNDEFINE_VARIABLE | ||||||||||||||||||||||||||||
| 256 | 621 | (?{ \&_match_undefine_variable }) ) /mx; | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # %DEFINE_SCRIPT | ||||||||||||||||||||||||||||
| 259 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% DEFINE_SCRIPT | ||||||||||||||||||||||||||||
| 260 | 621 | (?{ \&_match_define_script }) ) /mx; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # %UNDEFINE_ALL_SCRIPT | ||||||||||||||||||||||||||||
| 263 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% UNDEFINE_ALL_SCRIPT \s* | ||||||||||||||||||||||||||||
| 264 | 621 | (?{ \&_match_undefine_all_script }) ) /mx; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # %UNDEFINE_SCRIPT | ||||||||||||||||||||||||||||
| 267 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% UNDEFINE_SCRIPT | ||||||||||||||||||||||||||||
| 268 | 621 | (?{ \&_match_undefine_macro_script }) ) /mx; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | # %DEFINE | ||||||||||||||||||||||||||||
| 271 | 621 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% DEFINE (?{ \&_match_define_macro }) ) /mx; | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | # %UNDEFINE_ALL | ||||||||||||||||||||||||||||
| 274 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% UNDEFINE_ALL \s* | ||||||||||||||||||||||||||||
| 275 | 621 | (?{ \&_match_undefine_all_macro }) ) /mx; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # %UNDEFINE | ||||||||||||||||||||||||||||
| 278 | 621 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% UNDEFINE (?{ \&_match_undefine_macro_script }) ) /mx; | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # %CASE | ||||||||||||||||||||||||||||
| 281 | 621 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% CASE (?{ \&_match_case }) ) /mx; | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # %LOAD | ||||||||||||||||||||||||||||
| 284 | 621 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% LOAD (?{ \&_match_load }) ) /mx; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | # %INCLUDE | ||||||||||||||||||||||||||||
| 287 | 621 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% INCLUDE (?{ \&_match_include }) ) /mx; | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # %REQUIRE | ||||||||||||||||||||||||||||
| 290 | 621 | $regexp .= '|'.qr/ (?> ^ [\t ]* \% REQUIRE (?{ \&_match_require }) ) /mx; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | # concatenate operator | ||||||||||||||||||||||||||||
| 293 | 621 | $regexp .= '|'.qr/ (?> [\t ]* \# \# [\t ]* (?{ \&_match_concat }) ) /mx; | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # arguments to scripts | ||||||||||||||||||||||||||||
| 296 | 621 | $regexp .= '|'.qr/ (?> \# ( \d+ ) (?{ \&_match_expand_arg }) ) /mx; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # user actions reverse sorted by length, so that longest match is found | ||||||||||||||||||||||||||||
| 300 | 621 | my $actions = $self->actions; | |||||||||||||||||||||||||||
| 301 | 621 | for my $key (sort {length $b <=> length $a} keys %$actions) { | |||||||||||||||||||||||||||
| 302 | 1618 | $regexp .= '|'.qr/ (?> \Q$key\E (?{ \&_match_action }) ) /mx; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 621 | $regexp .= ')'; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 621 | $regexp = qr/$regexp/; | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 621 | $self->regexp($regexp); | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 313 | # match functions: called with matched text and following text; return new | ||||||||||||||||||||||||||||
| 314 | # following text | ||||||||||||||||||||||||||||
| 315 | 10 | ▶ | sub _match_escape { | ||||||||||||||||||||||||||
|
Subroutine
_match_escape: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 316 | 10 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 317 | 10 | $$output_ref .= $1; # special char is no longer parsed | |||||||||||||||||||||||||||
| 318 | 10 | return $input; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 5 | ▶ | sub _match_escape_newline { | ||||||||||||||||||||||||||
|
Subroutine
_match_escape_newline: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 322 | 5 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 323 | 5 | $$output_ref .= ' '; | |||||||||||||||||||||||||||
| 324 | 5 | return $input; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 3 | ▶ | sub _match_concat { | ||||||||||||||||||||||||||
|
Subroutine
_match_concat: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 328 | 3 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 329 | 3 | return $input; | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 63 | ▶ | sub _match_define_variable { | ||||||||||||||||||||||||||
|
Subroutine
_match_define_variable: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 333 | 63 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 63 | ▶ | $input =~ / [\t ]* ( $NAME_RE ) [\t ]* \[ /x | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | or $self->_error("Expected NAME [EXPR]"); | ||||||||||||||||||||||||||||
| 337 | 60 | my $name = $1; | |||||||||||||||||||||||||||
| 338 | 60 | $input = $'; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | # create a new context | ||||||||||||||||||||||||||||
| 341 | $self->_push_context(CTX_ARGS, | ||||||||||||||||||||||||||||
| 342 | 59 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 343 | 59 | my($rt_output_ref, @args) = @_; | |||||||||||||||||||||||||||
| 344 | 59 | ▶ | @args == 1 or $self->_error("Only one argument expected"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 58 | $self->define_variable($name, $args[0]); | |||||||||||||||||||||||||||
| 346 | }, | ||||||||||||||||||||||||||||
| 347 | undef, | ||||||||||||||||||||||||||||
| 348 | 60 | 1); | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | # change parser | ||||||||||||||||||||||||||||
| 351 | 60 | $self->parse_func( \&_parse_args ); | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 60 | return $input; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 24 | ▶ | sub _match_undefine { | ||||||||||||||||||||||||||
|
Subroutine
_match_undefine: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 357 | 24 | my($self, $input_ref) = @_; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 24 | ▶ | $$input_ref =~ / [\t ]* ( $NAME_RE ) \s* /x | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | or $self->_error("Expected NAME"); | ||||||||||||||||||||||||||||
| 361 | 18 | my $name = $1; | |||||||||||||||||||||||||||
| 362 | 18 | $$input_ref = $'; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 18 | return $name; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 8 | ▶ | sub _match_undefine_variable { | ||||||||||||||||||||||||||
|
Subroutine
_match_undefine_variable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 368 | 8 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 8 | my $name = $self->_match_undefine( \$input ); | |||||||||||||||||||||||||||
| 371 | 6 | $self->undefine_variable($name); | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 6 | return $input; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 3 | ▶ | sub _match_undefine_all_variable { | ||||||||||||||||||||||||||
|
Subroutine
_match_undefine_all_variable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 377 | 3 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 3 | $self->undefine_all_variable; | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 3 | return $input; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 158 | ▶ | sub _match_define_macro_script { | ||||||||||||||||||||||||||
|
Subroutine
_match_define_macro_script: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 385 | 158 | my($self, $output_ref, $match, $input, $is_script) = @_; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | # collect name | ||||||||||||||||||||||||||||
| 388 | 158 | ▶ | $input =~ / [\t ]* ( $NAME_RE ) [\t ]* /x | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | or $self->_error("Expected NAME"); | ||||||||||||||||||||||||||||
| 390 | 151 | my $name = $1; | |||||||||||||||||||||||||||
| 391 | 151 | $input = $'; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | # definition in the same line? | ||||||||||||||||||||||||||||
| 394 | 151 | ▶ | if ($input =~ /^ \[ /x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 124 | $input = $'; | |||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | # create a new context | ||||||||||||||||||||||||||||
| 398 | $self->_push_context(CTX_ARGS, | ||||||||||||||||||||||||||||
| 399 | 122 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 400 | 122 | my($rt_output_ref, @args) = @_; | |||||||||||||||||||||||||||
| 401 | 122 | ▶ | @args == 1 or $self->_error("Only one argument expected"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 402 | 122 | $self->_define_macro_script($name, $args[0], $is_script); | |||||||||||||||||||||||||||
| 403 | }, | ||||||||||||||||||||||||||||
| 404 | undef, | ||||||||||||||||||||||||||||
| 405 | 124 | 1); | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | # change parser | ||||||||||||||||||||||||||||
| 408 | 124 | $self->parse_func( \&_parse_args ); | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | else { | ||||||||||||||||||||||||||||
| 411 | 27 | $input =~ s/^\s+//; # eat newline | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | # collect text up to %END_DEFINE | ||||||||||||||||||||||||||||
| 414 | $self->_push_context(CTX_TEXT, | ||||||||||||||||||||||||||||
| 415 | 17 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 416 | 17 | my($rt_output_ref, $text) = @_; | |||||||||||||||||||||||||||
| 417 | 17 | $self->_define_macro_script($name, $text, $is_script); | |||||||||||||||||||||||||||
| 418 | }, | ||||||||||||||||||||||||||||
| 419 | 27 | qr/ ^ [\t ]* \% END_DEFINE \s* /mx, | |||||||||||||||||||||||||||
| 420 | 0); | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | # change parser | ||||||||||||||||||||||||||||
| 423 | 27 | $self->parse_func( \&_parse_collect_text ); | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 151 | return $input; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 99 | ▶ | sub _match_define_macro { | ||||||||||||||||||||||||||
|
Subroutine
_match_define_macro: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 430 | 99 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 431 | 99 | return $self->_match_define_macro_script($output_ref, $match, $input, 0); | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 14 | ▶ | sub _match_case { | ||||||||||||||||||||||||||
|
Subroutine
_match_case: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 435 | 14 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 14 | ▶ | $input =~ / [\t ]* \[ /x | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | or $self->_error("Expected [EXPR]"); | ||||||||||||||||||||||||||||
| 439 | 12 | $input = $'; | |||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | # create a new context | ||||||||||||||||||||||||||||
| 442 | $self->_push_context(CTX_ARGS, | ||||||||||||||||||||||||||||
| 443 | 12 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 444 | 12 | my($rt_output_ref, @args) = @_; | |||||||||||||||||||||||||||
| 445 | 12 | ▶ | @args == 1 or $self->_error("Only one argument expected"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | # compute expression | ||||||||||||||||||||||||||||
| 448 | 12 | my $case_arg = $self->_eval_expression($args[0]); | |||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | # collect text up to next %CASE or %END_CASE | ||||||||||||||||||||||||||||
| 451 | # or %CASE - in this case keep it in input, to be matched next | ||||||||||||||||||||||||||||
| 452 | $self->_push_context(CTX_TEXT, | ||||||||||||||||||||||||||||
| 453 | 9 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 454 | 9 | my($rt_output_ref, @args) = @_; | |||||||||||||||||||||||||||
| 455 | 9 | ▶ | @args == 1 or $self->_error("Only one argument expected"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 9 | ▶ | if ($case_arg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | 5 | my $body = $args[0]; | |||||||||||||||||||||||||||
| 459 | 5 | $body =~ s/^\s+//; # eat newline | |||||||||||||||||||||||||||
| 460 | 5 | $$rt_output_ref .= $self->_expand($body); | |||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | }, | ||||||||||||||||||||||||||||
| 463 | 10 | qr/ ^ [\t ]* \% END_CASE \s* | | |||||||||||||||||||||||||||
| 464 | (?= ^ [\t ]* \% CASE ) /mx, | ||||||||||||||||||||||||||||
| 465 | 0); | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | 10 | $self->parse_func( \&_parse_collect_text ); | |||||||||||||||||||||||||||
| 468 | }, | ||||||||||||||||||||||||||||
| 469 | undef, | ||||||||||||||||||||||||||||
| 470 | 12 | 1); | |||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | # change parser | ||||||||||||||||||||||||||||
| 473 | 12 | $self->parse_func( \&_parse_args ); | |||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 12 | return $input; | |||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 11 | ▶ | sub _match_filename { | ||||||||||||||||||||||||||
|
Subroutine
_match_filename: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 479 | 11 | my($self, $input, $func) = @_; | |||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | 11 | ▶ | $input =~ / [\t ]* \[ /x | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | or $self->_error("Expected [FILENAME]"); | ||||||||||||||||||||||||||||
| 483 | 9 | $input = $'; | |||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | # create a new context | ||||||||||||||||||||||||||||
| 486 | $self->_push_context(CTX_ARGS, | ||||||||||||||||||||||||||||
| 487 | 9 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 488 | 9 | my($rt_output_ref, @args) = @_; | |||||||||||||||||||||||||||
| 489 | 9 | ▶ | @args == 1 or $self->_error("Only one argument expected"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 490 | 9 | $self->$func($rt_output_ref, $args[0]); | |||||||||||||||||||||||||||
| 491 | }, | ||||||||||||||||||||||||||||
| 492 | undef, | ||||||||||||||||||||||||||||
| 493 | 9 | 1); | |||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | # change parser | ||||||||||||||||||||||||||||
| 496 | 9 | $self->parse_func( \&_parse_args ); | |||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 9 | return $input; | |||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | 5 | ▶ | sub _match_load { | ||||||||||||||||||||||||||
|
Subroutine
_match_load: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 502 | 5 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 503 | 5 | return $self->_match_filename($input, \&_load_file); | |||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | 4 | ▶ | sub _match_include { | ||||||||||||||||||||||||||
|
Subroutine
_match_include: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 507 | 4 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 508 | 4 | return $self->_match_filename($input, \&_expand_file); | |||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 2 | ▶ | sub _match_require { | ||||||||||||||||||||||||||
|
Subroutine
_match_require: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 512 | 2 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 513 | return $self->_match_filename($input, | ||||||||||||||||||||||||||||
| 514 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 515 | 2 | my($self, $output_ref, $file) = @_; | |||||||||||||||||||||||||||
| 516 | 2 | $self->_eval_expression("require '$file'"); | |||||||||||||||||||||||||||
| 517 | 2 | }); | |||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | 59 | ▶ | sub _match_define_script { | ||||||||||||||||||||||||||
|
Subroutine
_match_define_script: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 521 | 59 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 522 | 59 | return $self->_match_define_macro_script($output_ref, $match, $input, 1); | |||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | 16 | ▶ | sub _match_undefine_macro_script { | ||||||||||||||||||||||||||
|
Subroutine
_match_undefine_macro_script: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 526 | 16 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | 16 | my $name = $self->_match_undefine( \$input ); | |||||||||||||||||||||||||||
| 529 | 12 | $self->_undefine_macro_script($name); | |||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | 12 | return $input; | |||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 3 | ▶ | sub _match_undefine_all_macro { | ||||||||||||||||||||||||||
|
Subroutine
_match_undefine_all_macro: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 535 | 3 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | 3 | $self->undefine_all_macro; | |||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | 3 | return $input; | |||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | 3 | ▶ | sub _match_undefine_all_script { | ||||||||||||||||||||||||||
|
Subroutine
_match_undefine_all_script: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 543 | 3 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | 3 | $self->undefine_all_script; | |||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 3 | return $input; | |||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | 442 | ▶ | sub _match_action { | ||||||||||||||||||||||||||
|
Subroutine
_match_action: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 551 | 442 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | 442 | ▶ | my $func = $self->actions->{$match} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | or $self->_error("No action found for '$match'"); | ||||||||||||||||||||||||||||
| 555 | 442 | return $func->($self, $output_ref, $match, $input); | |||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | 111 | ▶ | sub _match_expand_arg { | ||||||||||||||||||||||||||
|
Subroutine
_match_expand_arg: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 559 | 111 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | 111 | my $arg = $1; | |||||||||||||||||||||||||||
| 562 | 111 | ▶ | ($arg < scalar(@{ $self->args })) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | or $self->_error("Missing parameters"); | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 97 | $$output_ref .= $self->_expand( $self->args->[$arg] ); | |||||||||||||||||||||||||||
| 566 | 97 | return $input; | |||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 570 | # match engine - recurse to expand all macros, return expanded text | ||||||||||||||||||||||||||||
| 571 | 1446 | ▶ | sub _expand { | ||||||||||||||||||||||||||
|
Subroutine
_expand: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 572 | 1446 | my($self, $input) = @_; | |||||||||||||||||||||||||||
| 573 | 1446 | ▶ | $input //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$input //= ""
MC/DC100%1/1
$input //= ""
$input
Truth table100%2/2
A
A= $input
| |||||||||||||||||||||||||||||
| 574 | 1446 | my $output = ''; | |||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | 1446 | ▶ | while ($input ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 577 | 2053 | $input = $self->parse_func->($self, \$output, $input); | |||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||
| 579 | 1389 | return $output; | |||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | # expand embedded text | ||||||||||||||||||||||||||||
| 583 | 176 | ▶ | sub _expand_embedded { | ||||||||||||||||||||||||||
|
Subroutine
_expand_embedded: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 584 | 176 | my($self, $input) = @_; | |||||||||||||||||||||||||||
| 585 | 176 | ▶ | $input //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$input //= ""
MC/DC100%1/1
$input //= ""
$input
Truth table100%2/2
A
A= $input
| |||||||||||||||||||||||||||||
| 586 | 176 | my $output = ''; | |||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | 176 | ▶ | while ($input ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 589 | 359 | ▶ | if ($self->in_embedded) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 590 | 170 | my $closedelim = $self->closedelim; | |||||||||||||||||||||||||||
| 591 | 170 | ▶ | if ($input =~ /\Q$closedelim\E/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 592 | 95 | $input = $'; | |||||||||||||||||||||||||||
| 593 | 95 | $output .= $self->_expand($`); | |||||||||||||||||||||||||||
| 594 | 95 | $self->in_embedded(0); | |||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||
| 596 | else { | ||||||||||||||||||||||||||||
| 597 | 75 | $output .= $self->_expand($input); | |||||||||||||||||||||||||||
| 598 | 75 | $input = ''; | |||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||
| 601 | else { | ||||||||||||||||||||||||||||
| 602 | 189 | my $opendelim = $self->opendelim; | |||||||||||||||||||||||||||
| 603 | 189 | ▶ | if ($input =~ /\Q$opendelim\E/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 604 | 95 | $output .= $`; | |||||||||||||||||||||||||||
| 605 | 95 | $input = $'; | |||||||||||||||||||||||||||
| 606 | 95 | $self->in_embedded(1); | |||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||
| 608 | else { | ||||||||||||||||||||||||||||
| 609 | 94 | $output .= $input; | |||||||||||||||||||||||||||
| 610 | 94 | $input = ''; | |||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||
| 614 | 176 | return $output; | |||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 618 | # choose either _expand or _expand_embedded | ||||||||||||||||||||||||||||
| 619 | 775 | ▶ | sub expand { | ||||||||||||||||||||||||||
|
Subroutine
expand: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 620 | 775 | my($self, $text, $file, $line_nr) = @_; | |||||||||||||||||||||||||||
| 621 | 775 | ▶ | defined($file) and $self->file($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 622 | 775 | ▶ | $line_nr and $self->line_nr($line_nr); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | 775 | ▶ | if ($self->embedded) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 625 | 176 | return $self->_expand_embedded($text); | |||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||
| 627 | else { | ||||||||||||||||||||||||||||
| 628 | 599 | return $self->_expand($text); | |||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | # parse functions: execute macros | ||||||||||||||||||||||||||||
| 635 | # input: text to parse and current output; | ||||||||||||||||||||||||||||
| 636 | # output: remaining text to parse and total text to output | ||||||||||||||||||||||||||||
| 637 | 1662 | ▶ | sub _parse_execute { | ||||||||||||||||||||||||||
|
Subroutine
_parse_execute: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 638 | 1662 | my($self, $output_ref, $input) = @_; | |||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | 1662 | ▶ | if ($input =~ / $self->{regexp} /x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 641 | 850 | my $action = $^R; | |||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | # execute action and set new input | ||||||||||||||||||||||||||||
| 644 | 850 | $$output_ref .= $`; | |||||||||||||||||||||||||||
| 645 | 850 | $input = $self->$action($output_ref, $&, $'); | |||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||
| 647 | else { | ||||||||||||||||||||||||||||
| 648 | 812 | $$output_ref .= $input; # remaining input | |||||||||||||||||||||||||||
| 649 | 812 | $input = ''; | |||||||||||||||||||||||||||
| 650 | } | ||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||
| 652 | 1622 | return $input; | |||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | # parse functions: collect macro arguments | ||||||||||||||||||||||||||||
| 656 | 304 | ▶ | sub _parse_args { | ||||||||||||||||||||||||||
|
Subroutine
_parse_args: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 657 | 304 | my($self, $output_ref, $input) = @_; | |||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | 304 | my $context = $self->_last_context_assert(CTX_ARGS); | |||||||||||||||||||||||||||
| 660 | 304 | ▶ | while ( $context->open_parens > 0 && $input ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$context->open_parens > 0 and $input ne ""
MC/DC100%2/2
$context->open_parens > 0 and $input ne ""
$context->open_parens > 0
$input ne ""
Truth table100%3/3
A and B
A= $context->open_parens > 0B= $input ne ""
| |||||||||||||||||||||||||||||
| 661 | 426 | ▶ | if ( $input =~ / | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 662 | (.*?) | ||||||||||||||||||||||||||||
| 663 | (?| (?> \\ ( [\[\]\|] ) (?{ \&_parse_args_escape }) ) | ||||||||||||||||||||||||||||
| 664 | | (?> ( \[ ) (?{ \&_parse_args_open }) ) | ||||||||||||||||||||||||||||
| 665 | | (?> ( \| ) (?{ \&_parse_args_separator }) ) | ||||||||||||||||||||||||||||
| 666 | | (?> ( \] ) (?{ \&_parse_args_close }) ) | ||||||||||||||||||||||||||||
| 667 | ) | ||||||||||||||||||||||||||||
| 668 | /sx ) { | ||||||||||||||||||||||||||||
| 669 | 418 | my $action = $^R; | |||||||||||||||||||||||||||
| 670 | 418 | $input = $'; # unparsed input | |||||||||||||||||||||||||||
| 671 | 418 | $action->($context); | |||||||||||||||||||||||||||
| 672 | } | ||||||||||||||||||||||||||||
| 673 | else { | ||||||||||||||||||||||||||||
| 674 | 8 | ▶ | @{ $context->args } or push @{ $context->args }, ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 675 | 8 | $context->args->[-1] .= $input; | |||||||||||||||||||||||||||
| 676 | 8 | $input = ''; | |||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | # check for end of parsing | ||||||||||||||||||||||||||||
| 681 | 304 | ▶ | if ( $context->open_parens == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 682 | 288 | $context->commit_func->($output_ref); | |||||||||||||||||||||||||||
| 683 | 271 | ▶ | $input =~ s/^\s+// if $context->eat_blanks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | 287 | return $input; | |||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | 5 | ▶ | sub _parse_args_escape { | ||||||||||||||||||||||||||
|
Subroutine
_parse_args_escape: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 690 | 5 | my($context) = @_; | |||||||||||||||||||||||||||
| 691 | 5 | ▶ | @{ $context->args } or push @{ $context->args }, ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 692 | 5 | $context->args->[-1] .= $1.$2; | |||||||||||||||||||||||||||
| 693 | } | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | 20 | ▶ | sub _parse_args_open { | ||||||||||||||||||||||||||
|
Subroutine
_parse_args_open: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 696 | 20 | my($context) = @_; | |||||||||||||||||||||||||||
| 697 | 20 | ▶ | @{ $context->args } or push @{ $context->args }, ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 698 | 20 | $context->args->[-1] .= $1.$2; | |||||||||||||||||||||||||||
| 699 | 20 | $context->{open_parens}++; | |||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | 85 | ▶ | sub _parse_args_separator { | ||||||||||||||||||||||||||
|
Subroutine
_parse_args_separator: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 703 | 85 | my($context) = @_; | |||||||||||||||||||||||||||
| 704 | 85 | ▶ | @{ $context->args } or push @{ $context->args }, ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 705 | 85 | ▶ | if ( $context->open_parens == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 706 | 74 | $context->args->[-1] .= $1; | |||||||||||||||||||||||||||
| 707 | 74 | push @{$context->args}, ''; | |||||||||||||||||||||||||||
| 708 | } | ||||||||||||||||||||||||||||
| 709 | else { | ||||||||||||||||||||||||||||
| 710 | 11 | $context->args->[-1] .= $1.$2; | |||||||||||||||||||||||||||
| 711 | } | ||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||
| 714 | 308 | ▶ | sub _parse_args_close { | ||||||||||||||||||||||||||
|
Subroutine
_parse_args_close: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 715 | 308 | my($context) = @_; | |||||||||||||||||||||||||||
| 716 | 308 | ▶ | @{ $context->args } or push @{ $context->args }, ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 717 | 308 | ▶ | if ( $context->open_parens == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 718 | 288 | $context->args->[-1] .= $1; | |||||||||||||||||||||||||||
| 719 | } | ||||||||||||||||||||||||||||
| 720 | else { | ||||||||||||||||||||||||||||
| 721 | 20 | $context->args->[-1] .= $1.$2; | |||||||||||||||||||||||||||
| 722 | } | ||||||||||||||||||||||||||||
| 723 | 308 | $context->{open_parens}--; | |||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||
| 726 | # Collect definition in text | ||||||||||||||||||||||||||||
| 727 | 87 | ▶ | sub _parse_collect_text { | ||||||||||||||||||||||||||
|
Subroutine
_parse_collect_text: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 728 | 87 | my($self, $output_ref, $input) = @_; | |||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||
| 730 | 87 | my $context = $self->_last_context_assert(CTX_TEXT); | |||||||||||||||||||||||||||
| 731 | 87 | ▶ | @{ $context->args } or push @{ $context->args }, ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 732 | 87 | my $end_text_re = $context->end_text_re; | |||||||||||||||||||||||||||
| 733 | 87 | ▶ | if ($input =~ /$end_text_re/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 734 | 26 | $context->args->[-1] .= $`; | |||||||||||||||||||||||||||
| 735 | 26 | $input = $'; | |||||||||||||||||||||||||||
| 736 | 26 | $context->commit_func->($output_ref); | |||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||
| 738 | else { | ||||||||||||||||||||||||||||
| 739 | 61 | $context->args->[-1] .= $input; | |||||||||||||||||||||||||||
| 740 | 61 | $input = ''; | |||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||
| 743 | 87 | return $input; | |||||||||||||||||||||||||||
| 744 | } | ||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 747 | # Define a new variable or overwrite an existing one | ||||||||||||||||||||||||||||
| 748 | 119 | ▶ | sub define_variable { | ||||||||||||||||||||||||||
|
Subroutine
define_variable: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 749 | 119 | my($self, $name, $value) = @_; | |||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||
| 751 | # setup for a possible recursive _expand(), if definition refers to itself | ||||||||||||||||||||||||||||
| 752 | # e.g. %DEFINE_VARIABLE X [#X + 1] | ||||||||||||||||||||||||||||
| 753 | 119 | ▶ | $self->variables->{$name} //= ''; # default previous value | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->variables->{$name} //= ""
MC/DC100%1/1
$self->variables->{$name} //= ""
$self->variables->{$name}
Truth table100%2/2
A
A= $self->variables->{$name}
| |||||||||||||||||||||||||||||
| 754 | 119 | $self->actions->{'#'.$name} = \&_expand_variable; | |||||||||||||||||||||||||||
| 755 | 119 | $self->_update_regexp; | |||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||
| 757 | 119 | $self->variables->{$name} = $self->_eval_expression($value, -ignore_errors); | |||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | 99 | ▶ | sub _expand_variable { | ||||||||||||||||||||||||||
|
Subroutine
_expand_variable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 761 | 99 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 762 | 99 | my $name = substr($match, 1); # skip '#' | |||||||||||||||||||||||||||
| 763 | 99 | $$output_ref .= $self->_expand( $self->variables->{$name} ); | |||||||||||||||||||||||||||
| 764 | 99 | return $input; | |||||||||||||||||||||||||||
| 765 | }; | ||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||
| 767 | 281 | ▶ | sub _eval_expression { | ||||||||||||||||||||||||||
|
Subroutine
_eval_expression: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 768 | 281 | my($self, $expression, $ignore_errors, @args) = @_; | |||||||||||||||||||||||||||
| 769 | 281 | my @save_args = @{ $self->args }; | |||||||||||||||||||||||||||
| 770 | 281 | $self->args( \@args ); # set arguments for this call | |||||||||||||||||||||||||||
| 771 | 281 | my @Param = @args; # to be used in script body | |||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | # expand any macro calls in the expression | ||||||||||||||||||||||||||||
| 774 | 281 | my $value = $self->_expand($expression); | |||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | 273 | my %Var = %{ $self->variables }; # to be used in script body | |||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | # try to eval as a perl expression, drop value on failure | ||||||||||||||||||||||||||||
| 779 | { | ||||||||||||||||||||||||||||
| 780 | 92 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 781 | 273 | my $eval_result = eval $value; | |||||||||||||||||||||||||||
| 782 | 273 | ▶ | if (! $@) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 783 | 232 | $value = $eval_result; | |||||||||||||||||||||||||||
| 784 | } | ||||||||||||||||||||||||||||
| 785 | elsif (! $ignore_errors) { | ||||||||||||||||||||||||||||
| 786 | 5 | my $error = $@; | |||||||||||||||||||||||||||
| 787 | 5 | $error =~ s/ at \(eval.*//; | |||||||||||||||||||||||||||
| 788 | 5 | $error =~ s/^Execution of .* aborted due to compilation errors.\n//m; | |||||||||||||||||||||||||||
| 789 | 5 | $self->_error("Eval error: $error"); | |||||||||||||||||||||||||||
| 790 | } | ||||||||||||||||||||||||||||
| 791 | } | ||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||
| 793 | 268 | %{ $self->variables } = %Var; # update any changed variables | |||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||
| 795 | 268 | $self->args( \@save_args ); # restore previous level args | |||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||
| 797 | 268 | return $value; | |||||||||||||||||||||||||||
| 798 | } | ||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||
| 800 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 801 | # Undefine a variable; does nothing if variable does not exist | ||||||||||||||||||||||||||||
| 802 | 21 | ▶ | sub undefine_variable { | ||||||||||||||||||||||||||
|
Subroutine
undefine_variable: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 803 | 21 | my($self, $name) = @_; | |||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | 21 | ▶ | if (exists $self->variables->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 806 | 11 | delete $self->variables->{$name}; | |||||||||||||||||||||||||||
| 807 | 11 | delete $self->actions->{'#'.$name}; | |||||||||||||||||||||||||||
| 808 | 11 | $self->_update_regexp; | |||||||||||||||||||||||||||
| 809 | } | ||||||||||||||||||||||||||||
| 810 | } | ||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||
| 812 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 813 | # Define a new script/macro or overwrite an existing one | ||||||||||||||||||||||||||||
| 814 | 250 | ▶ | sub _define_macro_script { | ||||||||||||||||||||||||||
|
Subroutine
_define_macro_script: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 815 | 250 | my($self, $name, $body, $is_script) = @_; | |||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | 250 | $self->macros->{$name} = $body; | |||||||||||||||||||||||||||
| 818 | 250 | $self->is_script->{$name} = $is_script; | |||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||
| 820 | 250 | $self->actions->{$name.'['} = \&_macro_script_collect_args; | |||||||||||||||||||||||||||
| 821 | 250 | $self->actions->{$name} = \&_macro_script_no_args; | |||||||||||||||||||||||||||
| 822 | 250 | $self->_update_regexp; | |||||||||||||||||||||||||||
| 823 | } | ||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||
| 825 | 86 | ▶ | sub _macro_script_collect_args { | ||||||||||||||||||||||||||
|
Subroutine
_macro_script_collect_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 826 | 86 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||
| 828 | 86 | my $name = substr($match, 0, length($match) - 1 ); # remove '[' | |||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||
| 830 | # create a new context | ||||||||||||||||||||||||||||
| 831 | $self->_push_context(CTX_ARGS, | ||||||||||||||||||||||||||||
| 832 | 86 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 833 | 86 | my($rt_output_ref, @args) = @_; | |||||||||||||||||||||||||||
| 834 | 86 | $self->_expand_macro_script($name, \@args, $rt_output_ref); | |||||||||||||||||||||||||||
| 835 | }, | ||||||||||||||||||||||||||||
| 836 | undef, | ||||||||||||||||||||||||||||
| 837 | 86 | 0); | |||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||
| 839 | # change parser | ||||||||||||||||||||||||||||
| 840 | 86 | $self->parse_func( \&_parse_args ); | |||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||
| 842 | 86 | return $input; | |||||||||||||||||||||||||||
| 843 | } | ||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||
| 845 | 257 | ▶ | sub _macro_script_no_args { | ||||||||||||||||||||||||||
|
Subroutine
_macro_script_no_args: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 846 | 257 | my($self, $output_ref, $match, $input) = @_; | |||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||
| 848 | 257 | my @args; | |||||||||||||||||||||||||||
| 849 | 257 | $self->_expand_macro_script($match, \@args, $output_ref); | |||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||
| 851 | 251 | return $input; | |||||||||||||||||||||||||||
| 852 | } | ||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||
| 854 | 343 | ▶ | sub _expand_macro_script { | ||||||||||||||||||||||||||
|
Subroutine
_expand_macro_script: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 855 | 343 | my($self, $name, $args, $output_ref) = @_; | |||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||
| 857 | 343 | ▶ | if ($self->is_script->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 858 | 148 | $$output_ref .= $self->_eval_expression( $self->macros->{$name}, 0, @$args ); | |||||||||||||||||||||||||||
| 859 | } | ||||||||||||||||||||||||||||
| 860 | else { | ||||||||||||||||||||||||||||
| 861 | 195 | my @save_args = @{ $self->args }; | |||||||||||||||||||||||||||
| 862 | 195 | $self->args( $args ); # set arguments for this call | |||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||
| 864 | 195 | $$output_ref .= $self->_expand( $self->macros->{$name} ); | |||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||
| 866 | 189 | $self->args( \@save_args ); # restore previous level args | |||||||||||||||||||||||||||
| 867 | } | ||||||||||||||||||||||||||||
| 868 | } | ||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||
| 870 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 871 | # Undefine a script/macro; does nothing if script/macro does not exist | ||||||||||||||||||||||||||||
| 872 | 31 | ▶ | sub _undefine_macro_script { | ||||||||||||||||||||||||||
|
Subroutine
_undefine_macro_script: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 873 | 31 | my($self, $name) = @_; | |||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||
| 875 | 31 | ▶ | if (exists $self->macros->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||
| 877 | 17 | delete $self->macros->{$name}; | |||||||||||||||||||||||||||
| 878 | 17 | delete $self->is_script->{$name}; | |||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||
| 880 | 17 | delete $self->actions->{$name.'['}; | |||||||||||||||||||||||||||
| 881 | 17 | delete $self->actions->{$name}; | |||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||
| 883 | 17 | $self->_update_regexp; | |||||||||||||||||||||||||||
| 884 | } | ||||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||
| 887 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 888 | # list_... | ||||||||||||||||||||||||||||
| 889 | # List objects to STDOUT or return to array, option -nameonly to list only name | ||||||||||||||||||||||||||||
| 890 | 111 | ▶ | sub _list_line { | ||||||||||||||||||||||||||
|
Subroutine
_list_line: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 891 | 111 | my($self, $define, $name, $body, $namesonly) = @_; | |||||||||||||||||||||||||||
| 892 | 111 | my $ret = "$define $name"; | |||||||||||||||||||||||||||
| 893 | 111 | ▶ | unless ($namesonly) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 894 | 54 | ▶ | if ($body =~ /\n/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 895 | 8 | chomp $body; | |||||||||||||||||||||||||||
| 896 | 8 | $ret .= "\n".$body."\n%END_DEFINE"; | |||||||||||||||||||||||||||
| 897 | } | ||||||||||||||||||||||||||||
| 898 | else { | ||||||||||||||||||||||||||||
| 899 | 46 | $ret .= " [$body]"; | |||||||||||||||||||||||||||
| 900 | } | ||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||
| 902 | 111 | $ret .= "\n"; | |||||||||||||||||||||||||||
| 903 | 111 | $ret; | |||||||||||||||||||||||||||
| 904 | } | ||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||
| 906 | 63 | ▶ | sub _list_lines { | ||||||||||||||||||||||||||
|
Subroutine
_list_lines: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 907 | 63 | my($self, $define, $items, $namesonly, $output_ref) = @_; | |||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||
| 909 | 63 | my @sorted_items = sort { $a->[0] cmp $b->[0] } @$items; | |||||||||||||||||||||||||||
| 910 | 63 | for (@sorted_items) { | |||||||||||||||||||||||||||
| 911 | 111 | my($name, $body) = @$_; | |||||||||||||||||||||||||||
| 912 | 111 | my $line = $self->_list_line($define, $name, $body, $namesonly); | |||||||||||||||||||||||||||
| 913 | 111 | ▶ | if ($output_ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 914 | 48 | push @$output_ref, $line; | |||||||||||||||||||||||||||
| 915 | } | ||||||||||||||||||||||||||||
| 916 | else { | ||||||||||||||||||||||||||||
| 917 | 63 | print $line; | |||||||||||||||||||||||||||
| 918 | } | ||||||||||||||||||||||||||||
| 919 | } | ||||||||||||||||||||||||||||
| 920 | } | ||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||
| 922 | 21 | ▶ | sub list_variable { | ||||||||||||||||||||||||||
|
Subroutine
list_variable: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 923 | 21 | my($self, $namesonly) = @_; | |||||||||||||||||||||||||||
| 924 | 21 | my @lines; | |||||||||||||||||||||||||||
| 925 | my @items; | ||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||
| 927 | 21 | ▶ | while (my($name, $body) = each %{ $self->variables }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 928 | 37 | push @items, [$name, $body]; | |||||||||||||||||||||||||||
| 929 | } | ||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||
| 931 | 21 | ▶ | $self->_list_lines("%DEFINE_VARIABLE", \@items, $namesonly, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 932 | wantarray ? \@lines : undef ); | ||||||||||||||||||||||||||||
| 933 | 21 | ▶ | return @lines if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 934 | } | ||||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||
| 936 | 42 | ▶ | sub _list_macro_script { | ||||||||||||||||||||||||||
|
Subroutine
_list_macro_script: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 937 | 42 | my($self, $define, $is_script, $namesonly) = @_; | |||||||||||||||||||||||||||
| 938 | 42 | my @lines; | |||||||||||||||||||||||||||
| 939 | my @items; | ||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||
| 941 | 42 | ▶ | while (my($name, $body) = each %{ $self->macros }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 942 | 116 | ▶ | push @items, [$name, $body] if !! $self->is_script->{$name} == !! $is_script; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 943 | } | ||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||
| 945 | 42 | ▶ | $self->_list_lines($define, \@items, $namesonly, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 946 | wantarray ? \@lines : undef ); | ||||||||||||||||||||||||||||
| 947 | 42 | ▶ | return @lines if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 948 | } | ||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||
| 950 | 21 | ▶ | sub list_macro { | ||||||||||||||||||||||||||
|
Subroutine
list_macro: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 951 | 21 | my($self, $namesonly) = @_; | |||||||||||||||||||||||||||
| 952 | 21 | $self->_list_macro_script("%DEFINE", 0, $namesonly); | |||||||||||||||||||||||||||
| 953 | } | ||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||
| 955 | 21 | ▶ | sub list_script { | ||||||||||||||||||||||||||
|
Subroutine
list_script: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 956 | 21 | my($self, $namesonly) = @_; | |||||||||||||||||||||||||||
| 957 | 21 | $self->_list_macro_script("%DEFINE_SCRIPT", 1, $namesonly); | |||||||||||||||||||||||||||
| 958 | } | ||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||
| 960 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 961 | # load macro definitions from a file | ||||||||||||||||||||||||||||
| 962 | 49 | ▶ | sub _load_file { | ||||||||||||||||||||||||||
|
Subroutine
_load_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 963 | 49 | my($self, $output_ref, $file) = @_; | |||||||||||||||||||||||||||
| 964 | |||||||||||||||||||||||||||||
| 965 | # Treat loaded files as if wrapped in delimiters (only affects embedded | ||||||||||||||||||||||||||||
| 966 | # processing). | ||||||||||||||||||||||||||||
| 967 | 49 | my $in_embedded = $self->in_embedded; | |||||||||||||||||||||||||||
| 968 | 49 | $self->in_embedded(1); | |||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||
| 970 | 49 | $self->_expand_file(undef, $file); # never output | |||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||
| 972 | 46 | $self->in_embedded($in_embedded); | |||||||||||||||||||||||||||
| 973 | } | ||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||
| 975 | 45 | ▶ | sub load_file { | ||||||||||||||||||||||||||
|
Subroutine
load_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 976 | 45 | my($self, $file) = @_; | |||||||||||||||||||||||||||
| 977 | 45 | $self->_load_file(undef, $file); | |||||||||||||||||||||||||||
| 978 | } | ||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||
| 980 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 981 | # parses the given file with expand() | ||||||||||||||||||||||||||||
| 982 | # Usage: $macro->expand_file($filename) | ||||||||||||||||||||||||||||
| 983 | # In an array context will return the file, e.g. | ||||||||||||||||||||||||||||
| 984 | # @expanded = $macro->expand_file($filename); | ||||||||||||||||||||||||||||
| 985 | # In a void context will print to the current output filehandle | ||||||||||||||||||||||||||||
| 986 | 138 | ▶ | sub _expand_file { | ||||||||||||||||||||||||||
|
Subroutine
_expand_file: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 987 | 138 | my($self, $output_ref, $file) = @_; | |||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||
| 989 | # let Path::Tiny handle '~' processing | ||||||||||||||||||||||||||||
| 990 | 138 | ▶ | $file or croak "Missing filename"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 991 | 137 | $file = path($file); | |||||||||||||||||||||||||||
| 992 | |||||||||||||||||||||||||||||
| 993 | 137 | ▶ | open(my $fh, $file) or $self->_error("Open '$file' failed: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 994 | 133 | my $line_nr; | |||||||||||||||||||||||||||
| 995 | |||||||||||||||||||||||||||||
| 996 | # define function to collect output | ||||||||||||||||||||||||||||
| 997 | my $output; | ||||||||||||||||||||||||||||
| 998 | 133 | ▶ | if (! defined($output_ref)) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 999 | 47 | ▶ | $output = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1000 | } | ||||||||||||||||||||||||||||
| 1001 | elsif (ref($output_ref) eq 'SCALAR') { | ||||||||||||||||||||||||||||
| 1002 | 2 | ▶ | $output = sub { $$output_ref .= $_[0]; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1003 | } | ||||||||||||||||||||||||||||
| 1004 | elsif (ref($output_ref) eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 1005 | 79 | ▶ | $output = sub { push @$output_ref, $_[0]; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1006 | } | ||||||||||||||||||||||||||||
| 1007 | elsif (ref($output_ref) eq 'GLOB') { | ||||||||||||||||||||||||||||
| 1008 | 5 | ▶ | $output = sub { print $_[0]; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1009 | } | ||||||||||||||||||||||||||||
| 1010 | else { | ||||||||||||||||||||||||||||
| 1011 | 0 | croak("invalid output_ref"); | |||||||||||||||||||||||||||
| 1012 | } | ||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||
| 1014 | # read input | ||||||||||||||||||||||||||||
| 1015 | 133 | ▶ | while(defined(my $line = <$fh>)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1016 | 280 | $line_nr++; | |||||||||||||||||||||||||||
| 1017 | 280 | $line = $self->expand($line, $file, $line_nr); | |||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||
| 1019 | 274 | ▶ | $output->($line) if $line ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1020 | } | ||||||||||||||||||||||||||||
| 1021 | |||||||||||||||||||||||||||||
| 1022 | 127 | ▶ | close($fh) or croak "Close '$file' failed: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1023 | } | ||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||
| 1025 | 86 | ▶ | sub expand_file { | ||||||||||||||||||||||||||
|
Subroutine
expand_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1026 | 86 | my($self, $file) = @_; | |||||||||||||||||||||||||||
| 1027 | 86 | my @lines; | |||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||
| 1029 | # build output destination | ||||||||||||||||||||||||||||
| 1030 | 86 | ▶ | my $output_ref = wantarray ? \@lines : \*STDOUT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1031 | 86 | $self->_expand_file($output_ref, $file); | |||||||||||||||||||||||||||
| 1032 | 80 | ▶ | return @lines if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1033 | } | ||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||
| 1035 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 1036 | # Wrappers for script/macro | ||||||||||||||||||||||||||||
| 1037 | 56 | ▶ | sub define_macro { | ||||||||||||||||||||||||||
|
Subroutine
define_macro: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1038 | 56 | my($self, $name, $body) = @_; | |||||||||||||||||||||||||||
| 1039 | 56 | $self->_define_macro_script($name, $body, 0); | |||||||||||||||||||||||||||
| 1040 | } | ||||||||||||||||||||||||||||
| 1041 | |||||||||||||||||||||||||||||
| 1042 | 55 | ▶ | sub define_script { | ||||||||||||||||||||||||||
|
Subroutine
define_script: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1043 | 55 | my($self, $name, $body) = @_; | |||||||||||||||||||||||||||
| 1044 | 55 | $self->_define_macro_script($name, $body, 1); | |||||||||||||||||||||||||||
| 1045 | } | ||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||
| 1047 | 92 | *undefine_macro = \&_undefine_macro_script; | |||||||||||||||||||||||||||
| 1048 | 92 | *undefine_script = \&_undefine_macro_script; | |||||||||||||||||||||||||||
| 1049 | |||||||||||||||||||||||||||||
| 1050 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 1051 | # define the standard %% comment macro | ||||||||||||||||||||||||||||
| 1052 | 7 | ▶ | sub _define_standard_comment { | ||||||||||||||||||||||||||
|
Subroutine
_define_standard_comment: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 1053 | 7 | my($self) = @_; | |||||||||||||||||||||||||||
| 1054 | 7 | $self->define_macro($COMMENT, ''); | |||||||||||||||||||||||||||
| 1055 | } | ||||||||||||||||||||||||||||
| 1056 | |||||||||||||||||||||||||||||
| 1057 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 1058 | # Undefine all ... | ||||||||||||||||||||||||||||
| 1059 | 16 | ▶ | sub _undefine_all_macro_script { | ||||||||||||||||||||||||||
|
Subroutine
_undefine_all_macro_script: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 1060 | 16 | my($self, $is_script) = @_; | |||||||||||||||||||||||||||
| 1061 | |||||||||||||||||||||||||||||
| 1062 | # delete all keys first and update regexp at the end | ||||||||||||||||||||||||||||
| 1063 | # do not call _undefine_macro_script to avoid recomputing the regexp | ||||||||||||||||||||||||||||
| 1064 | # after each deleted macro | ||||||||||||||||||||||||||||
| 1065 | 16 | for my $name (keys %{ $self->macros }) { | |||||||||||||||||||||||||||
| 1066 | 51 | ▶ | if ( !! $is_script == !! $self->is_script->{$name} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1067 | 39 | delete $self->macros->{$name}; | |||||||||||||||||||||||||||
| 1068 | 39 | delete $self->is_script->{$name}; | |||||||||||||||||||||||||||
| 1069 | 39 | delete $self->actions->{$name.'['}; | |||||||||||||||||||||||||||
| 1070 | 39 | delete $self->actions->{$name}; | |||||||||||||||||||||||||||
| 1071 | } | ||||||||||||||||||||||||||||
| 1072 | } | ||||||||||||||||||||||||||||
| 1073 | 16 | $self->_update_regexp; | |||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||
| 1075 | # redefine comment macro | ||||||||||||||||||||||||||||
| 1076 | 16 | ▶ | $self->_define_standard_comment if $self->comment; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 1077 | } | ||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||
| 1079 | 8 | ▶ | sub undefine_all_macro { | ||||||||||||||||||||||||||
|
Subroutine
undefine_all_macro: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1080 | 8 | my($self) = @_; | |||||||||||||||||||||||||||
| 1081 | 8 | $self->_undefine_all_macro_script(0); | |||||||||||||||||||||||||||
| 1082 | } | ||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||
| 1084 | 8 | ▶ | sub undefine_all_script { | ||||||||||||||||||||||||||
|
Subroutine
undefine_all_script: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 1085 | 8 | my($self) = @_; | |||||||||||||||||||||||||||
| 1086 | 8 | $self->_undefine_all_macro_script(1); | |||||||||||||||||||||||||||
| 1087 | } | ||||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||
| 1089 | 7 | ▶ | sub undefine_all_variable { | ||||||||||||||||||||||||||
|
Subroutine
undefine_all_variable: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 1090 | 7 | my($self) = @_; | |||||||||||||||||||||||||||
| 1091 | |||||||||||||||||||||||||||||
| 1092 | # delete all keys first and update regexp at the end | ||||||||||||||||||||||||||||
| 1093 | # do not call _undefine_macro_script to avoid recomputing the regexp | ||||||||||||||||||||||||||||
| 1094 | # after each deleted macro | ||||||||||||||||||||||||||||
| 1095 | 7 | for my $name (keys %{ $self->variables }) { | |||||||||||||||||||||||||||
| 1096 | 27 | delete $self->variables->{$name}; | |||||||||||||||||||||||||||
| 1097 | 27 | delete $self->actions->{'#'.$name}; | |||||||||||||||||||||||||||
| 1098 | } | ||||||||||||||||||||||||||||
| 1099 | 7 | $self->_update_regexp; | |||||||||||||||||||||||||||
| 1100 | } | ||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||
| 1102 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 1103 | # deprecated method to define -macro, -script or -variable | ||||||||||||||||||||||||||||
| 1104 | 19 | ▶ | sub define { | ||||||||||||||||||||||||||
|
Subroutine
define: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1105 | 19 | my($self, $which, $name, $body) = @_; | |||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||
| 1107 | 19 | ▶ | if ($which eq '-variable') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 1108 | 4 | $self->define_variable($name, $body); | |||||||||||||||||||||||||||
| 1109 | } | ||||||||||||||||||||||||||||
| 1110 | elsif ($which eq '-macro') { | ||||||||||||||||||||||||||||
| 1111 | 3 | $self->define_macro($name, $body); | |||||||||||||||||||||||||||
| 1112 | } | ||||||||||||||||||||||||||||
| 1113 | elsif ($which eq '-script') { | ||||||||||||||||||||||||||||
| 1114 | 11 | $self->define_script($name, $body); | |||||||||||||||||||||||||||
| 1115 | } | ||||||||||||||||||||||||||||
| 1116 | else { | ||||||||||||||||||||||||||||
| 1117 | 1 | croak "$which method not supported"; | |||||||||||||||||||||||||||
| 1118 | } | ||||||||||||||||||||||||||||
| 1119 | } | ||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||
| 1121 | 16 | ▶ | sub undefine { | ||||||||||||||||||||||||||
|
Subroutine
undefine: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1122 | 16 | my($self, $which, $name) = @_; | |||||||||||||||||||||||||||
| 1123 | |||||||||||||||||||||||||||||
| 1124 | 16 | ▶ | if ($which eq '-variable') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 1125 | 4 | $self->undefine_variable($name); | |||||||||||||||||||||||||||
| 1126 | } | ||||||||||||||||||||||||||||
| 1127 | elsif ($which eq '-macro') { | ||||||||||||||||||||||||||||
| 1128 | 4 | $self->undefine_macro($name); | |||||||||||||||||||||||||||
| 1129 | } | ||||||||||||||||||||||||||||
| 1130 | elsif ($which eq '-script') { | ||||||||||||||||||||||||||||
| 1131 | 5 | $self->undefine_script($name); | |||||||||||||||||||||||||||
| 1132 | } | ||||||||||||||||||||||||||||
| 1133 | else { | ||||||||||||||||||||||||||||
| 1134 | 3 | croak "$which method not supported"; | |||||||||||||||||||||||||||
| 1135 | } | ||||||||||||||||||||||||||||
| 1136 | } | ||||||||||||||||||||||||||||
| 1137 | |||||||||||||||||||||||||||||
| 1138 | 12 | ▶ | sub undefine_all { | ||||||||||||||||||||||||||
|
Subroutine
undefine_all: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1139 | 12 | my($self, $which) = @_; | |||||||||||||||||||||||||||
| 1140 | 12 | ▶ | $which //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$which //= ""
MC/DC100%1/1
$which //= ""
$which
Truth table100%2/2
A
A= $which
| |||||||||||||||||||||||||||||
| 1141 | |||||||||||||||||||||||||||||
| 1142 | 12 | ▶ | if ($which eq '-variable') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 1143 | 2 | $self->undefine_all_variable; | |||||||||||||||||||||||||||
| 1144 | } | ||||||||||||||||||||||||||||
| 1145 | elsif ($which eq '-macro') { | ||||||||||||||||||||||||||||
| 1146 | 2 | $self->undefine_all_macro; | |||||||||||||||||||||||||||
| 1147 | } | ||||||||||||||||||||||||||||
| 1148 | elsif ($which eq '-script') { | ||||||||||||||||||||||||||||
| 1149 | 3 | $self->undefine_all_script; | |||||||||||||||||||||||||||
| 1150 | } | ||||||||||||||||||||||||||||
| 1151 | else { | ||||||||||||||||||||||||||||
| 1152 | 5 | croak "$which method not supported"; | |||||||||||||||||||||||||||
| 1153 | } | ||||||||||||||||||||||||||||
| 1154 | } | ||||||||||||||||||||||||||||
| 1155 | |||||||||||||||||||||||||||||
| 1156 | 42 | ▶ | sub list { | ||||||||||||||||||||||||||
|
Subroutine
list: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 1157 | 42 | my($self, $which, $namesonly) = @_; | |||||||||||||||||||||||||||
| 1158 | 42 | ▶ | $which //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$which //= ""
MC/DC100%1/1
$which //= ""
$which
Truth table100%2/2
A
A= $which
| |||||||||||||||||||||||||||||
| 1159 | |||||||||||||||||||||||||||||
| 1160 | 42 | ▶ | if ($which eq '-variable') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 1161 | 13 | $self->list_variable($namesonly); | |||||||||||||||||||||||||||
| 1162 | } | ||||||||||||||||||||||||||||
| 1163 | elsif ($which eq '-macro') { | ||||||||||||||||||||||||||||
| 1164 | 13 | $self->list_macro($namesonly); | |||||||||||||||||||||||||||
| 1165 | } | ||||||||||||||||||||||||||||
| 1166 | elsif ($which eq '-script') { | ||||||||||||||||||||||||||||
| 1167 | 13 | $self->list_script($namesonly); | |||||||||||||||||||||||||||
| 1168 | } | ||||||||||||||||||||||||||||
| 1169 | else { | ||||||||||||||||||||||||||||
| 1170 | 3 | croak "$which method not supported"; | |||||||||||||||||||||||||||
| 1171 | } | ||||||||||||||||||||||||||||
| 1172 | } | ||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||
| 1174 | 92 | 1; | |||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||
| 1176 | =head1 NAME | ||||||||||||||||||||||||||||
| 1177 | |||||||||||||||||||||||||||||
| 1178 | Text::MacroScript - A macro pre-processor with embedded perl capability | ||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||
| 1180 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||
| 1182 | use Text::MacroScript; | ||||||||||||||||||||||||||||
| 1183 | |||||||||||||||||||||||||||||
| 1184 | # new() for macro processing | ||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||
| 1186 | my $Macro = Text::MacroScript->new; | ||||||||||||||||||||||||||||
| 1187 | while( <> ) { | ||||||||||||||||||||||||||||
| 1188 | print $Macro->expand( $_ ) if $_; | ||||||||||||||||||||||||||||
| 1189 | } | ||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||
| 1191 | # Canonical use (the filename and line number improves error messages): | ||||||||||||||||||||||||||||
| 1192 | my $Macro = Text::MacroScript->new; | ||||||||||||||||||||||||||||
| 1193 | while( <> ) { | ||||||||||||||||||||||||||||
| 1194 | print $Macro->expand( $_, $ARGV, $. ) if $_; | ||||||||||||||||||||||||||||
| 1195 | } | ||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||
| 1197 | # new() for embedded macro processing | ||||||||||||||||||||||||||||
| 1198 | |||||||||||||||||||||||||||||
| 1199 | my $Macro = Text::MacroScript->new( -embedded => 1 ); | ||||||||||||||||||||||||||||
| 1200 | # Delimiters default to <: and :> | ||||||||||||||||||||||||||||
| 1201 | # or | ||||||||||||||||||||||||||||
| 1202 | my $Macro = Text::MacroScript->new( -opendelim => '[[', -closedelim => ']]' ); | ||||||||||||||||||||||||||||
| 1203 | while( <> ) { | ||||||||||||||||||||||||||||
| 1204 | print $Macro->expand( $_, $ARGV, $. ) if $_; | ||||||||||||||||||||||||||||
| 1205 | } | ||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||
| 1207 | # Create a macro object and create initial macros/scripts from the file(s) | ||||||||||||||||||||||||||||
| 1208 | # given: | ||||||||||||||||||||||||||||
| 1209 | my $Macro = Text::MacroScript->new( | ||||||||||||||||||||||||||||
| 1210 | -file => [ 'local.macro', '~/.macro/global.macro' ] | ||||||||||||||||||||||||||||
| 1211 | ); | ||||||||||||||||||||||||||||
| 1212 | |||||||||||||||||||||||||||||
| 1213 | # Create a macro object and create initial macros/scripts from the | ||||||||||||||||||||||||||||
| 1214 | # definition(s) given: | ||||||||||||||||||||||||||||
| 1215 | my $Macro = Text::MacroScript->new( | ||||||||||||||||||||||||||||
| 1216 | -macro => [ | ||||||||||||||||||||||||||||
| 1217 | [ 'MAX_INT' => '32767' ], | ||||||||||||||||||||||||||||
| 1218 | ], | ||||||||||||||||||||||||||||
| 1219 | -script => [ | ||||||||||||||||||||||||||||
| 1220 | [ 'DHM2S' => | ||||||||||||||||||||||||||||
| 1221 | [ | ||||||||||||||||||||||||||||
| 1222 | my $s = (#0*24*60*60)+(#1*60*60)+(#2*60); | ||||||||||||||||||||||||||||
| 1223 | "#0 days, #1 hrs, #2 mins = $s secs" | ||||||||||||||||||||||||||||
| 1224 | ], | ||||||||||||||||||||||||||||
| 1225 | ], | ||||||||||||||||||||||||||||
| 1226 | -variable => [ '*MARKER*' => 0 ], | ||||||||||||||||||||||||||||
| 1227 | ); | ||||||||||||||||||||||||||||
| 1228 | |||||||||||||||||||||||||||||
| 1229 | # We may of course use any combination of the options. | ||||||||||||||||||||||||||||
| 1230 | |||||||||||||||||||||||||||||
| 1231 | my $Macro = Text::MacroScript->new( -comment => 1 ); # Create the %%[] macro. | ||||||||||||||||||||||||||||
| 1232 | |||||||||||||||||||||||||||||
| 1233 | # define() | ||||||||||||||||||||||||||||
| 1234 | $Macro->define_macro( $macroname, $macrobody ); | ||||||||||||||||||||||||||||
| 1235 | $Macro->define_script( $scriptname, $scriptbody ); | ||||||||||||||||||||||||||||
| 1236 | $Macro->define_variable( $variablename, $variablebody ); | ||||||||||||||||||||||||||||
| 1237 | |||||||||||||||||||||||||||||
| 1238 | # undefine() | ||||||||||||||||||||||||||||
| 1239 | $Macro->undefine_macro( $macroname ); | ||||||||||||||||||||||||||||
| 1240 | $Macro->undefine_script( $scriptname ); | ||||||||||||||||||||||||||||
| 1241 | $Macro->undefine_variable( $variablename ); | ||||||||||||||||||||||||||||
| 1242 | |||||||||||||||||||||||||||||
| 1243 | # undefine_all() | ||||||||||||||||||||||||||||
| 1244 | $Macro->undefine_all_macro; | ||||||||||||||||||||||||||||
| 1245 | $Macro->undefine_all_script; | ||||||||||||||||||||||||||||
| 1246 | $Macro->undefine_all_variable; | ||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||
| 1248 | # list() | ||||||||||||||||||||||||||||
| 1249 | @macros = $Macro->list_macro; | ||||||||||||||||||||||||||||
| 1250 | @macros = $Macro->list_macro( -namesonly ); | ||||||||||||||||||||||||||||
| 1251 | |||||||||||||||||||||||||||||
| 1252 | @scripts = $Macro->list_script; | ||||||||||||||||||||||||||||
| 1253 | @scripts = $Macro->list_script( -namesonly ); | ||||||||||||||||||||||||||||
| 1254 | |||||||||||||||||||||||||||||
| 1255 | @variables = $Macro->list_variable; | ||||||||||||||||||||||||||||
| 1256 | @variables = $Macro->list_variable( -namesonly ); | ||||||||||||||||||||||||||||
| 1257 | |||||||||||||||||||||||||||||
| 1258 | # load_file() - always treats the contents as within delimiters if we are | ||||||||||||||||||||||||||||
| 1259 | # doing embedded processing. | ||||||||||||||||||||||||||||
| 1260 | |||||||||||||||||||||||||||||
| 1261 | $Macro->load_file( $filename ); | ||||||||||||||||||||||||||||
| 1262 | |||||||||||||||||||||||||||||
| 1263 | # expand_file() - calls expand() for each input line. | ||||||||||||||||||||||||||||
| 1264 | $Macro->expand_file( $filename ); | ||||||||||||||||||||||||||||
| 1265 | @expanded = $Macro->expand_file( $filename ); | ||||||||||||||||||||||||||||
| 1266 | |||||||||||||||||||||||||||||
| 1267 | # expand() | ||||||||||||||||||||||||||||
| 1268 | $expanded = $Macro->expand( $unexpanded ); | ||||||||||||||||||||||||||||
| 1269 | $expanded = $Macro->expand( $unexpanded, $filename, $line_nr ); | ||||||||||||||||||||||||||||
| 1270 | |||||||||||||||||||||||||||||
| 1271 | This bundle also includes the C<macropp> and C<macrodir> scripts which allows us | ||||||||||||||||||||||||||||
| 1272 | to expand macros without having to use/understand C<Text::MacroScript>, | ||||||||||||||||||||||||||||
| 1273 | although you will have to learn the handful of macro commands available and | ||||||||||||||||||||||||||||
| 1274 | which are documented here and in C<macropp>. C<macropp> provides more | ||||||||||||||||||||||||||||
| 1275 | documentation on the embedded approach. | ||||||||||||||||||||||||||||
| 1276 | |||||||||||||||||||||||||||||
| 1277 | The C<macroutil.pl> library supplied provides some functions which you may | ||||||||||||||||||||||||||||
| 1278 | choose to use in HTML work for example. | ||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||
| 1280 | =head1 MACRO SYSTEMS VS EMBEDDED SYSTEMS | ||||||||||||||||||||||||||||
| 1281 | |||||||||||||||||||||||||||||
| 1282 | Macro systems read all the text, substituting anything which matches a macro | ||||||||||||||||||||||||||||
| 1283 | name with the macro's body (or script name with the result of the execution of | ||||||||||||||||||||||||||||
| 1284 | the script). This makes macro systems slower (they have to check for | ||||||||||||||||||||||||||||
| 1285 | macro/script names everywhere, not just in a delimited section) and more risky | ||||||||||||||||||||||||||||
| 1286 | (if we choose a macro/script name that normally occurs in the text we'll end | ||||||||||||||||||||||||||||
| 1287 | up with a mess) than embedded systems. On the other hand because they work on | ||||||||||||||||||||||||||||
| 1288 | the whole text not just delimited bits, macro systems can perform processing | ||||||||||||||||||||||||||||
| 1289 | that embedded systems can't. Macro systems are used extensively, for example | ||||||||||||||||||||||||||||
| 1290 | the CPP, C pre-processor, with its #DEFINE's, etc. | ||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||
| 1292 | Essentially, embedded systems print all text until they hit an opening | ||||||||||||||||||||||||||||
| 1293 | delimiter. They then execute any code up until the closing delimiter. The text | ||||||||||||||||||||||||||||
| 1294 | that results replaces everything between and including the delimeters. They | ||||||||||||||||||||||||||||
| 1295 | then carry on printing text until they hit an opening delimeter and so on | ||||||||||||||||||||||||||||
| 1296 | until they've finished processing all the text. This module now provides both | ||||||||||||||||||||||||||||
| 1297 | approaches. | ||||||||||||||||||||||||||||
| 1298 | |||||||||||||||||||||||||||||
| 1299 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 1300 | |||||||||||||||||||||||||||||
| 1301 | Define macros, scripts and variables in macro files or directly in text files. | ||||||||||||||||||||||||||||
| 1302 | |||||||||||||||||||||||||||||
| 1303 | Commands can appear in separate macro files which are loaded in either via the | ||||||||||||||||||||||||||||
| 1304 | text files they process (e.g. via the L</%LOAD> command), or can be embedded | ||||||||||||||||||||||||||||
| 1305 | directly in text files. Almost every command that can appear in a file has an | ||||||||||||||||||||||||||||
| 1306 | equivalent object method so that programmers can achieve the same things in | ||||||||||||||||||||||||||||
| 1307 | code as can be achieved by macro commands in texts; there are also additional | ||||||||||||||||||||||||||||
| 1308 | methods which have no command equivalents. | ||||||||||||||||||||||||||||
| 1309 | |||||||||||||||||||||||||||||
| 1310 | Most the examples given here use the macro approach. However this module now | ||||||||||||||||||||||||||||
| 1311 | directly supports an embedded approach and this is now documented. Although | ||||||||||||||||||||||||||||
| 1312 | you can specify your own delimiters where shown in examples we use the default | ||||||||||||||||||||||||||||
| 1313 | delimiters of C<E<lt>:> and C<:E<gt>> throughout. | ||||||||||||||||||||||||||||
| 1314 | |||||||||||||||||||||||||||||
| 1315 | =head2 Public methods | ||||||||||||||||||||||||||||
| 1316 | |||||||||||||||||||||||||||||
| 1317 | =head3 new | ||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||
| 1319 | $self = Text::MacroScript->new(); | ||||||||||||||||||||||||||||
| 1320 | $self = Text::MacroScript->new( %opts ); | ||||||||||||||||||||||||||||
| 1321 | |||||||||||||||||||||||||||||
| 1322 | Create a new C<Text::MacroScript> object, initialized with the supplied | ||||||||||||||||||||||||||||
| 1323 | options. By default creates an object for macro processing. | ||||||||||||||||||||||||||||
| 1324 | |||||||||||||||||||||||||||||
| 1325 | For macro processing: | ||||||||||||||||||||||||||||
| 1326 | |||||||||||||||||||||||||||||
| 1327 | my $Macro = Text::MacroScript->new; | ||||||||||||||||||||||||||||
| 1328 | |||||||||||||||||||||||||||||
| 1329 | For embedded macro processing: | ||||||||||||||||||||||||||||
| 1330 | |||||||||||||||||||||||||||||
| 1331 | my $Macro = Text::MacroScript->new( -embedded => 1 ); | ||||||||||||||||||||||||||||
| 1332 | # Delimiters default to <: and :> | ||||||||||||||||||||||||||||
| 1333 | |||||||||||||||||||||||||||||
| 1334 | Or specify your own delimiters: | ||||||||||||||||||||||||||||
| 1335 | |||||||||||||||||||||||||||||
| 1336 | my $Macro = Text::MacroScript->new( -opendelim => '[[', -closedelim => ']]' ); | ||||||||||||||||||||||||||||
| 1337 | |||||||||||||||||||||||||||||
| 1338 | Or specify one delimiter to use for both (probably not wise): | ||||||||||||||||||||||||||||
| 1339 | |||||||||||||||||||||||||||||
| 1340 | my $Macro = Text::MacroScript->new( -opendelim => '%%' ); | ||||||||||||||||||||||||||||
| 1341 | # -closedelim defaults to -opendelim, e.g. %% in this case | ||||||||||||||||||||||||||||
| 1342 | |||||||||||||||||||||||||||||
| 1343 | The full list of options that can be specified at object creation: | ||||||||||||||||||||||||||||
| 1344 | |||||||||||||||||||||||||||||
| 1345 | =over 4 | ||||||||||||||||||||||||||||
| 1346 | |||||||||||||||||||||||||||||
| 1347 | =item * | ||||||||||||||||||||||||||||
| 1348 | |||||||||||||||||||||||||||||
| 1349 | C<-embedded =E<gt> 1> | ||||||||||||||||||||||||||||
| 1350 | |||||||||||||||||||||||||||||
| 1351 | Create the object for embedded processing, with default C<E<lt>:> and | ||||||||||||||||||||||||||||
| 1352 | C<:E<gt>> delimiters. If option value is C<0>, or if the option is not | ||||||||||||||||||||||||||||
| 1353 | supplied, create the object for macro processing. | ||||||||||||||||||||||||||||
| 1354 | |||||||||||||||||||||||||||||
| 1355 | =item * | ||||||||||||||||||||||||||||
| 1356 | |||||||||||||||||||||||||||||
| 1357 | C<-opendelim =E<gt> '[[', -closedelim =E<gt> ']]'> | ||||||||||||||||||||||||||||
| 1358 | |||||||||||||||||||||||||||||
| 1359 | Create the object for embedded processing, with the supplied C<[[> and | ||||||||||||||||||||||||||||
| 1360 | C<]]> delimiters. | ||||||||||||||||||||||||||||
| 1361 | |||||||||||||||||||||||||||||
| 1362 | =item * | ||||||||||||||||||||||||||||
| 1363 | |||||||||||||||||||||||||||||
| 1364 | C<-opendelim =E<gt> '%%'> | ||||||||||||||||||||||||||||
| 1365 | |||||||||||||||||||||||||||||
| 1366 | Create the object for embedded processing, with the same C<!!> as open | ||||||||||||||||||||||||||||
| 1367 | and close delimiters. | ||||||||||||||||||||||||||||
| 1368 | |||||||||||||||||||||||||||||
| 1369 | =item * | ||||||||||||||||||||||||||||
| 1370 | |||||||||||||||||||||||||||||
| 1371 | C<-comment =E<gt> 1> | ||||||||||||||||||||||||||||
| 1372 | |||||||||||||||||||||||||||||
| 1373 | Create the C<%%[]> comment macro. | ||||||||||||||||||||||||||||
| 1374 | |||||||||||||||||||||||||||||
| 1375 | =item * | ||||||||||||||||||||||||||||
| 1376 | |||||||||||||||||||||||||||||
| 1377 | C<-file =E<gt> [ @files ]> | ||||||||||||||||||||||||||||
| 1378 | |||||||||||||||||||||||||||||
| 1379 | See also L</%LOAD> and C<macropp -f>. | ||||||||||||||||||||||||||||
| 1380 | |||||||||||||||||||||||||||||
| 1381 | =item * | ||||||||||||||||||||||||||||
| 1382 | |||||||||||||||||||||||||||||
| 1383 | C<-macro =E<gt> [ @macros ]> | ||||||||||||||||||||||||||||
| 1384 | |||||||||||||||||||||||||||||
| 1385 | Define macros, where each macro is a pair of C<name =E<gt> body>, e.g. | ||||||||||||||||||||||||||||
| 1386 | |||||||||||||||||||||||||||||
| 1387 | my $Macro = Text::MacroScript->new(-macro => [ ["name1"=>"body1"], ["name2"=>"body2"] ] ); | ||||||||||||||||||||||||||||
| 1388 | |||||||||||||||||||||||||||||
| 1389 | See also L</%DEFINE>. | ||||||||||||||||||||||||||||
| 1390 | |||||||||||||||||||||||||||||
| 1391 | =item * | ||||||||||||||||||||||||||||
| 1392 | |||||||||||||||||||||||||||||
| 1393 | C<-script =E<gt> [ @scripts ]> | ||||||||||||||||||||||||||||
| 1394 | |||||||||||||||||||||||||||||
| 1395 | Define scripts, where each script is a pair of C<name =E<gt> body>, e.g. | ||||||||||||||||||||||||||||
| 1396 | |||||||||||||||||||||||||||||
| 1397 | my $Macro = Text::MacroScript->new(-script => [ ["name1"=>"body1"], ["name2"=>"body2"] ] ); | ||||||||||||||||||||||||||||
| 1398 | |||||||||||||||||||||||||||||
| 1399 | See also L</%DEFINE_SCRIPT>. | ||||||||||||||||||||||||||||
| 1400 | |||||||||||||||||||||||||||||
| 1401 | =item * | ||||||||||||||||||||||||||||
| 1402 | |||||||||||||||||||||||||||||
| 1403 | C<-variable =E<gt> [ @svariables ]> | ||||||||||||||||||||||||||||
| 1404 | |||||||||||||||||||||||||||||
| 1405 | Define variables, where each variable is a pair of C<name =E<gt> value>, e.g. | ||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||
| 1407 | my $Macro = Text::MacroScript->new(-variable => [ ["name1"=>"value1"], ["name2"=>"value2"] ] ); | ||||||||||||||||||||||||||||
| 1408 | |||||||||||||||||||||||||||||
| 1409 | See also L</%DEFINE_VARIABLE>. | ||||||||||||||||||||||||||||
| 1410 | |||||||||||||||||||||||||||||
| 1411 | =back | ||||||||||||||||||||||||||||
| 1412 | |||||||||||||||||||||||||||||
| 1413 | =head3 define_macro | ||||||||||||||||||||||||||||
| 1414 | |||||||||||||||||||||||||||||
| 1415 | $Macro->define_macro( $name, $body ); | ||||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||
| 1417 | Defines a macro with the given name that expands to the given body when | ||||||||||||||||||||||||||||
| 1418 | called. If a macro with the same name already exists, it is silently | ||||||||||||||||||||||||||||
| 1419 | overwritten. | ||||||||||||||||||||||||||||
| 1420 | |||||||||||||||||||||||||||||
| 1421 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1422 | |||||||||||||||||||||||||||||
| 1423 | $Macro->define( -macro, $name, $body ); | ||||||||||||||||||||||||||||
| 1424 | |||||||||||||||||||||||||||||
| 1425 | See also L</%DEFINE>. | ||||||||||||||||||||||||||||
| 1426 | |||||||||||||||||||||||||||||
| 1427 | =head3 list_macro | ||||||||||||||||||||||||||||
| 1428 | |||||||||||||||||||||||||||||
| 1429 | $Macro->list_macro; # lists to STDOUT | ||||||||||||||||||||||||||||
| 1430 | @output = $Macro->list_macro; # lists to array | ||||||||||||||||||||||||||||
| 1431 | $Macro->list_macro(-namesonly); # only names | ||||||||||||||||||||||||||||
| 1432 | |||||||||||||||||||||||||||||
| 1433 | Lists all defined macros to C<STDOUT> or returns the result if called in | ||||||||||||||||||||||||||||
| 1434 | list context. Accepts an optional parameter C<-namesonly> to list only | ||||||||||||||||||||||||||||
| 1435 | the macro names and not the body. | ||||||||||||||||||||||||||||
| 1436 | |||||||||||||||||||||||||||||
| 1437 | =head3 undefine_macro | ||||||||||||||||||||||||||||
| 1438 | |||||||||||||||||||||||||||||
| 1439 | $Macro->undefine_macro( $name ); | ||||||||||||||||||||||||||||
| 1440 | |||||||||||||||||||||||||||||
| 1441 | If a macro exists with the given name, it is deleted. If not, the function | ||||||||||||||||||||||||||||
| 1442 | does nothing. | ||||||||||||||||||||||||||||
| 1443 | |||||||||||||||||||||||||||||
| 1444 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1445 | |||||||||||||||||||||||||||||
| 1446 | $Macro->undefine( -macro, $name ); | ||||||||||||||||||||||||||||
| 1447 | |||||||||||||||||||||||||||||
| 1448 | See also L</%UNDEFINE>. | ||||||||||||||||||||||||||||
| 1449 | |||||||||||||||||||||||||||||
| 1450 | =head3 undefine_all_macro | ||||||||||||||||||||||||||||
| 1451 | |||||||||||||||||||||||||||||
| 1452 | $Macro->undefine_all_macro; | ||||||||||||||||||||||||||||
| 1453 | |||||||||||||||||||||||||||||
| 1454 | Delete all the defined macros. | ||||||||||||||||||||||||||||
| 1455 | |||||||||||||||||||||||||||||
| 1456 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1457 | |||||||||||||||||||||||||||||
| 1458 | $Macro->undefine_all( -macro ); | ||||||||||||||||||||||||||||
| 1459 | |||||||||||||||||||||||||||||
| 1460 | See also L</%UNDEFINE_ALL>. | ||||||||||||||||||||||||||||
| 1461 | |||||||||||||||||||||||||||||
| 1462 | =cut | ||||||||||||||||||||||||||||
| 1463 | # $Macro->define_macro( $name, \@arg_names, $body ); | ||||||||||||||||||||||||||||
| 1464 | #The optional array of C<@arg_names> contains the names of local variables | ||||||||||||||||||||||||||||
| 1465 | #that are defined with the actual arguments passed to the macro when called. | ||||||||||||||||||||||||||||
| 1466 | #The arguments are refered in the body as other variables, prefixed with | ||||||||||||||||||||||||||||
| 1467 | #C<#>, e.g. | ||||||||||||||||||||||||||||
| 1468 | # | ||||||||||||||||||||||||||||
| 1469 | # $Macro->define_macro( 'ADD', ['A', 'B'], "#A+#B" ); | ||||||||||||||||||||||||||||
| 1470 | # $Macro->expand("ADD[2|3]"); --> "2+3" | ||||||||||||||||||||||||||||
| 1471 | |||||||||||||||||||||||||||||
| 1472 | =head3 define_script | ||||||||||||||||||||||||||||
| 1473 | |||||||||||||||||||||||||||||
| 1474 | $Macro->define_script( $name, $body ); | ||||||||||||||||||||||||||||
| 1475 | |||||||||||||||||||||||||||||
| 1476 | Defines a perl script with the given name that executes the given body | ||||||||||||||||||||||||||||
| 1477 | when called. If a script with the same name already exists, it is | ||||||||||||||||||||||||||||
| 1478 | silently overwritten. | ||||||||||||||||||||||||||||
| 1479 | |||||||||||||||||||||||||||||
| 1480 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1481 | |||||||||||||||||||||||||||||
| 1482 | $Macro->define( -script, $name, $body ); | ||||||||||||||||||||||||||||
| 1483 | |||||||||||||||||||||||||||||
| 1484 | See also L</%DEFINE_SCRIPT>. | ||||||||||||||||||||||||||||
| 1485 | |||||||||||||||||||||||||||||
| 1486 | =head3 list_script | ||||||||||||||||||||||||||||
| 1487 | |||||||||||||||||||||||||||||
| 1488 | $Macro->list_script; # lists to STDOUT | ||||||||||||||||||||||||||||
| 1489 | @output = $Macro->list_script; # lists to array | ||||||||||||||||||||||||||||
| 1490 | $Macro->list_script(-namesonly); # only names | ||||||||||||||||||||||||||||
| 1491 | |||||||||||||||||||||||||||||
| 1492 | Lists all defined scripts to C<STDOUT> or returns the result if called in | ||||||||||||||||||||||||||||
| 1493 | list context. Accepts an optional parameter C<-namesonly> to list only | ||||||||||||||||||||||||||||
| 1494 | the script names and not the body. | ||||||||||||||||||||||||||||
| 1495 | |||||||||||||||||||||||||||||
| 1496 | =head3 undefine_script | ||||||||||||||||||||||||||||
| 1497 | |||||||||||||||||||||||||||||
| 1498 | $Macro->undefine_script( $name ); | ||||||||||||||||||||||||||||
| 1499 | |||||||||||||||||||||||||||||
| 1500 | If a script exists with the given name, it is deleted. If not, the function | ||||||||||||||||||||||||||||
| 1501 | does nothing. | ||||||||||||||||||||||||||||
| 1502 | |||||||||||||||||||||||||||||
| 1503 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1504 | |||||||||||||||||||||||||||||
| 1505 | $Macro->undefine( -script, $name ); | ||||||||||||||||||||||||||||
| 1506 | |||||||||||||||||||||||||||||
| 1507 | See also L</%UNDEFINE_SCRIPT>. | ||||||||||||||||||||||||||||
| 1508 | |||||||||||||||||||||||||||||
| 1509 | =head3 undefine_all_script | ||||||||||||||||||||||||||||
| 1510 | |||||||||||||||||||||||||||||
| 1511 | $Macro->undefine_all_script; | ||||||||||||||||||||||||||||
| 1512 | |||||||||||||||||||||||||||||
| 1513 | Delete all the defined scripts. | ||||||||||||||||||||||||||||
| 1514 | |||||||||||||||||||||||||||||
| 1515 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1516 | |||||||||||||||||||||||||||||
| 1517 | $Macro->undefine_all( -script ); | ||||||||||||||||||||||||||||
| 1518 | |||||||||||||||||||||||||||||
| 1519 | See also L</%UNDEFINE_ALL_SCRIPT>. | ||||||||||||||||||||||||||||
| 1520 | |||||||||||||||||||||||||||||
| 1521 | =cut | ||||||||||||||||||||||||||||
| 1522 | # $Macro->define_script( $name, \@arg_names, $body ); | ||||||||||||||||||||||||||||
| 1523 | # | ||||||||||||||||||||||||||||
| 1524 | #The optional array of C<@arg_names> contains the names of local variables | ||||||||||||||||||||||||||||
| 1525 | #that are defined with the actual arguments passed to the script when called. | ||||||||||||||||||||||||||||
| 1526 | #The arguments are referred in the body as other variables, prefixed with | ||||||||||||||||||||||||||||
| 1527 | #C<#>, e.g. | ||||||||||||||||||||||||||||
| 1528 | # | ||||||||||||||||||||||||||||
| 1529 | # $Macro->define_script( 'ADD', ['A', 'B'], "#A+#B" ); | ||||||||||||||||||||||||||||
| 1530 | # $Macro->expand("ADD[2|3]"); --> "5" | ||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||
| 1532 | =head3 define_variable | ||||||||||||||||||||||||||||
| 1533 | |||||||||||||||||||||||||||||
| 1534 | $Macro->define_variable( $name, $value ); | ||||||||||||||||||||||||||||
| 1535 | |||||||||||||||||||||||||||||
| 1536 | Defines or updates a variable that can be used within macros or perl scripts | ||||||||||||||||||||||||||||
| 1537 | as C<#varname>. | ||||||||||||||||||||||||||||
| 1538 | |||||||||||||||||||||||||||||
| 1539 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1540 | |||||||||||||||||||||||||||||
| 1541 | $Macro->define( -variable, $name, $value ); | ||||||||||||||||||||||||||||
| 1542 | |||||||||||||||||||||||||||||
| 1543 | See also L</%DEFINE_VARIABLE>. | ||||||||||||||||||||||||||||
| 1544 | |||||||||||||||||||||||||||||
| 1545 | =head3 list_variable | ||||||||||||||||||||||||||||
| 1546 | |||||||||||||||||||||||||||||
| 1547 | $Macro->list_variable; # lists to STDOUT | ||||||||||||||||||||||||||||
| 1548 | @output = $Macro->list_variable; # lists to array | ||||||||||||||||||||||||||||
| 1549 | $Macro->list_variable(-namesonly); # only names | ||||||||||||||||||||||||||||
| 1550 | |||||||||||||||||||||||||||||
| 1551 | Lists all defined variables to C<STDOUT> or returns the result if called in | ||||||||||||||||||||||||||||
| 1552 | list context. Accepts an optional parameter C<-namesonly> to list only | ||||||||||||||||||||||||||||
| 1553 | the variable names and not the body. | ||||||||||||||||||||||||||||
| 1554 | |||||||||||||||||||||||||||||
| 1555 | =head3 undefine_variable | ||||||||||||||||||||||||||||
| 1556 | |||||||||||||||||||||||||||||
| 1557 | $Macro->undefine_variable( $name ); | ||||||||||||||||||||||||||||
| 1558 | |||||||||||||||||||||||||||||
| 1559 | If a variable exists with the given name, it is deleted. If not, the function | ||||||||||||||||||||||||||||
| 1560 | does nothing. | ||||||||||||||||||||||||||||
| 1561 | |||||||||||||||||||||||||||||
| 1562 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1563 | |||||||||||||||||||||||||||||
| 1564 | $Macro->undefine( -variable, $name ); | ||||||||||||||||||||||||||||
| 1565 | |||||||||||||||||||||||||||||
| 1566 | See also L</%UNDEFINE_VARIABLE>. | ||||||||||||||||||||||||||||
| 1567 | |||||||||||||||||||||||||||||
| 1568 | =head3 undefine_all_variable | ||||||||||||||||||||||||||||
| 1569 | |||||||||||||||||||||||||||||
| 1570 | $Macro->undefine_all_variable; | ||||||||||||||||||||||||||||
| 1571 | |||||||||||||||||||||||||||||
| 1572 | Delete all the defined variables. | ||||||||||||||||||||||||||||
| 1573 | |||||||||||||||||||||||||||||
| 1574 | This is the same as the deprecated syntax: | ||||||||||||||||||||||||||||
| 1575 | |||||||||||||||||||||||||||||
| 1576 | $Macro->undefine_all( -variable ); | ||||||||||||||||||||||||||||
| 1577 | |||||||||||||||||||||||||||||
| 1578 | See also L</%UNDEFINE_ALL_VARIABLE>. | ||||||||||||||||||||||||||||
| 1579 | |||||||||||||||||||||||||||||
| 1580 | =head3 expand | ||||||||||||||||||||||||||||
| 1581 | |||||||||||||||||||||||||||||
| 1582 | $text = $Macro->expand( $in ); | ||||||||||||||||||||||||||||
| 1583 | $text = $Macro->expand( $in, $filename, $line_nr ); | ||||||||||||||||||||||||||||
| 1584 | |||||||||||||||||||||||||||||
| 1585 | Expands the given C<$in> input and returns the expanded text. The C<$in> | ||||||||||||||||||||||||||||
| 1586 | is either a text line or an interator that returns a sequence of text | ||||||||||||||||||||||||||||
| 1587 | lines. | ||||||||||||||||||||||||||||
| 1588 | |||||||||||||||||||||||||||||
| 1589 | The C<$filename> is optional and defaults to C<"-">. The <$line_nr> is | ||||||||||||||||||||||||||||
| 1590 | optional and defaults to C<1>. They are used in error messages to locate | ||||||||||||||||||||||||||||
| 1591 | the error. | ||||||||||||||||||||||||||||
| 1592 | |||||||||||||||||||||||||||||
| 1593 | The expansion processes any macro definitions and expands any macro | ||||||||||||||||||||||||||||
| 1594 | calls found in the input text. C<expand()> buffers internally all the | ||||||||||||||||||||||||||||
| 1595 | lines required for a multi-line definition, i.e. it can be called once | ||||||||||||||||||||||||||||
| 1596 | for each line of a multi-line L</%DEFINE>. | ||||||||||||||||||||||||||||
| 1597 | |||||||||||||||||||||||||||||
| 1598 | =head3 load_file | ||||||||||||||||||||||||||||
| 1599 | |||||||||||||||||||||||||||||
| 1600 | $Macro->load_file( $filename ); | ||||||||||||||||||||||||||||
| 1601 | |||||||||||||||||||||||||||||
| 1602 | See also L</%LOAD> and C<macropp -f>. | ||||||||||||||||||||||||||||
| 1603 | |||||||||||||||||||||||||||||
| 1604 | =head3 expand_file | ||||||||||||||||||||||||||||
| 1605 | |||||||||||||||||||||||||||||
| 1606 | $Macro->expand_file( $filename ); | ||||||||||||||||||||||||||||
| 1607 | @expanded = $Macro->expand_file( $filename ); | ||||||||||||||||||||||||||||
| 1608 | |||||||||||||||||||||||||||||
| 1609 | When called in C<void> context, sends output to the current output | ||||||||||||||||||||||||||||
| 1610 | filehandle. When called in C<ARRAY> context, returns the list of | ||||||||||||||||||||||||||||
| 1611 | expaned lines. | ||||||||||||||||||||||||||||
| 1612 | |||||||||||||||||||||||||||||
| 1613 | Calls C<expand()> on each line of the file. | ||||||||||||||||||||||||||||
| 1614 | |||||||||||||||||||||||||||||
| 1615 | See also L</%INCLUDE>. | ||||||||||||||||||||||||||||
| 1616 | |||||||||||||||||||||||||||||
| 1617 | =head1 MACRO LANGUAGE | ||||||||||||||||||||||||||||
| 1618 | |||||||||||||||||||||||||||||
| 1619 | This chapter describes the macro language statements processed in the | ||||||||||||||||||||||||||||
| 1620 | input files. | ||||||||||||||||||||||||||||
| 1621 | |||||||||||||||||||||||||||||
| 1622 | =head2 Defining and using macros | ||||||||||||||||||||||||||||
| 1623 | |||||||||||||||||||||||||||||
| 1624 | These commands can appear in separate I<macro> files, and/or in the body of | ||||||||||||||||||||||||||||
| 1625 | files. Wherever a macroname or scriptname is encountered it will be replaced | ||||||||||||||||||||||||||||
| 1626 | by the body of the macro or the result of the evaluation of the script using | ||||||||||||||||||||||||||||
| 1627 | any parameters that are given. | ||||||||||||||||||||||||||||
| 1628 | |||||||||||||||||||||||||||||
| 1629 | Note that if we are using an embedded approach commands, macro names and | ||||||||||||||||||||||||||||
| 1630 | script names should appear between delimiters. (Except when we L</%LOAD> since | ||||||||||||||||||||||||||||
| 1631 | this assumes the whole file is I<embedded>. | ||||||||||||||||||||||||||||
| 1632 | |||||||||||||||||||||||||||||
| 1633 | =head3 %DEFINE | ||||||||||||||||||||||||||||
| 1634 | |||||||||||||||||||||||||||||
| 1635 | %DEFINE macroname [macro body] | ||||||||||||||||||||||||||||
| 1636 | %DEFINE macroname | ||||||||||||||||||||||||||||
| 1637 | multi-line | ||||||||||||||||||||||||||||
| 1638 | macro body | ||||||||||||||||||||||||||||
| 1639 | #0, #1 are the first and second parameters if any used | ||||||||||||||||||||||||||||
| 1640 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1641 | |||||||||||||||||||||||||||||
| 1642 | Thus, in the body of a file we may have, for example: | ||||||||||||||||||||||||||||
| 1643 | |||||||||||||||||||||||||||||
| 1644 | %DEFINE &B [Billericky Rickety Builders] | ||||||||||||||||||||||||||||
| 1645 | Some arbitrary text. | ||||||||||||||||||||||||||||
| 1646 | We are writing to complain to the &B about the shoddy work they did. | ||||||||||||||||||||||||||||
| 1647 | |||||||||||||||||||||||||||||
| 1648 | If we are taking the embedded approach the example above might become: | ||||||||||||||||||||||||||||
| 1649 | |||||||||||||||||||||||||||||
| 1650 | <:%DEFINE BB [Billericky Rickety Builders]:> | ||||||||||||||||||||||||||||
| 1651 | Some arbitrary text. | ||||||||||||||||||||||||||||
| 1652 | We are writing to complain to the <:BB:> about the shoddy work they did. | ||||||||||||||||||||||||||||
| 1653 | |||||||||||||||||||||||||||||
| 1654 | When using an embedded approach we don't have to make the macro or script name | ||||||||||||||||||||||||||||
| 1655 | unique within the text, (although each must be distinct from each other), | ||||||||||||||||||||||||||||
| 1656 | since the delimiters are used to signify them. However since expansion applies | ||||||||||||||||||||||||||||
| 1657 | recursively it is still wise to make names distinctive. | ||||||||||||||||||||||||||||
| 1658 | |||||||||||||||||||||||||||||
| 1659 | In files we would write: | ||||||||||||||||||||||||||||
| 1660 | |||||||||||||||||||||||||||||
| 1661 | %DEFINE MAC [The Mackintosh Macro] | ||||||||||||||||||||||||||||
| 1662 | |||||||||||||||||||||||||||||
| 1663 | The equivalent method call is: | ||||||||||||||||||||||||||||
| 1664 | |||||||||||||||||||||||||||||
| 1665 | $Macro->define_macro( 'MAC', 'The Mackintosh Macro' ); | ||||||||||||||||||||||||||||
| 1666 | |||||||||||||||||||||||||||||
| 1667 | We can call our macro anything, excluding white-space and special | ||||||||||||||||||||||||||||
| 1668 | characters used while parsing the input text (C<[,],(,),#>). | ||||||||||||||||||||||||||||
| 1669 | |||||||||||||||||||||||||||||
| 1670 | All names are case-sensitive. | ||||||||||||||||||||||||||||
| 1671 | |||||||||||||||||||||||||||||
| 1672 | So a name like C<%*&!> is fine - indeed names which | ||||||||||||||||||||||||||||
| 1673 | could not normally appear in the text are recommended to avoid having the | ||||||||||||||||||||||||||||
| 1674 | wrong thing substituted. We should also avoid calling macros, scripts or | ||||||||||||||||||||||||||||
| 1675 | variables names beginning with C<#>. | ||||||||||||||||||||||||||||
| 1676 | |||||||||||||||||||||||||||||
| 1677 | Note that if we define a macro and then a script with the same name the | ||||||||||||||||||||||||||||
| 1678 | script will effectively replace the macro. | ||||||||||||||||||||||||||||
| 1679 | |||||||||||||||||||||||||||||
| 1680 | We can have parameters (for macros and scripts), e.g.: | ||||||||||||||||||||||||||||
| 1681 | |||||||||||||||||||||||||||||
| 1682 | %DEFINE *P [The forename is #0 and the surname is #1] | ||||||||||||||||||||||||||||
| 1683 | |||||||||||||||||||||||||||||
| 1684 | Parameters used in the source text can contain square brackets since macro | ||||||||||||||||||||||||||||
| 1685 | will grab up to the last square bracket on the line. The only thing we can't | ||||||||||||||||||||||||||||
| 1686 | pass are C<|>s since these are used to separate parameters. White-space between | ||||||||||||||||||||||||||||
| 1687 | the macro name and the C<[> is optional in definitions but I<not allowed> in the | ||||||||||||||||||||||||||||
| 1688 | source text. | ||||||||||||||||||||||||||||
| 1689 | |||||||||||||||||||||||||||||
| 1690 | Parameters are named C<#0>, C<#1>, etc. There is a limit of 100 parameters, i.e. | ||||||||||||||||||||||||||||
| 1691 | C<#0..#99>, and we must use all those we specify. In the example above we I<must> | ||||||||||||||||||||||||||||
| 1692 | use C<*P[param1|param2]>, e.g. C<*P[Jim|Hendrix]>; if we don't | ||||||||||||||||||||||||||||
| 1693 | C<Text::MacroScript> will croak. Note that macro names and their parameters | ||||||||||||||||||||||||||||
| 1694 | must all be on the same line (although this is relaxed if you use paragraph | ||||||||||||||||||||||||||||
| 1695 | mode). | ||||||||||||||||||||||||||||
| 1696 | |||||||||||||||||||||||||||||
| 1697 | Because we use C<#> to signify parameters if you require text that consists of a | ||||||||||||||||||||||||||||
| 1698 | C<#> followed by digits then you should escape the C<#>, e.g. | ||||||||||||||||||||||||||||
| 1699 | |||||||||||||||||||||||||||||
| 1700 | %DEFINE *GRAY[<font color="\#121212">#0</font>] | ||||||||||||||||||||||||||||
| 1701 | |||||||||||||||||||||||||||||
| 1702 | We can use as many I<more> parameters than we need, for example add a third to | ||||||||||||||||||||||||||||
| 1703 | document: C<*P[Jim|Hendrix|Musician]> will become | ||||||||||||||||||||||||||||
| 1704 | I<'The forename is Jim and the surname is Hendrix'>, | ||||||||||||||||||||||||||||
| 1705 | just as in the previous example; the third parameter, | ||||||||||||||||||||||||||||
| 1706 | I<'Musician'>, will simply be thrown away. | ||||||||||||||||||||||||||||
| 1707 | |||||||||||||||||||||||||||||
| 1708 | If we take an embedded approach we might write this example thus: | ||||||||||||||||||||||||||||
| 1709 | |||||||||||||||||||||||||||||
| 1710 | <:%DEFINE P [The forename is #0 and the surname is #1]:> | ||||||||||||||||||||||||||||
| 1711 | |||||||||||||||||||||||||||||
| 1712 | and in the text, <:P[Jim|Hendrix]:> will be transformed appropriately. | ||||||||||||||||||||||||||||
| 1713 | |||||||||||||||||||||||||||||
| 1714 | If we define a macro, script or variable and later define the same name the | ||||||||||||||||||||||||||||
| 1715 | later definition will replace the earlier one. This is useful for making local | ||||||||||||||||||||||||||||
| 1716 | macro definitions over-ride global ones, simply by loading the global ones | ||||||||||||||||||||||||||||
| 1717 | first. | ||||||||||||||||||||||||||||
| 1718 | |||||||||||||||||||||||||||||
| 1719 | Although macros can have plain textual names like this: | ||||||||||||||||||||||||||||
| 1720 | |||||||||||||||||||||||||||||
| 1721 | %DEFINE MAX_INT [32767] | ||||||||||||||||||||||||||||
| 1722 | |||||||||||||||||||||||||||||
| 1723 | It is generally wise to use a prefix and/or suffix to make sure we don't | ||||||||||||||||||||||||||||
| 1724 | expand something unintentionally, e.g. | ||||||||||||||||||||||||||||
| 1725 | |||||||||||||||||||||||||||||
| 1726 | %DEFINE $MAX_INT [65535] | ||||||||||||||||||||||||||||
| 1727 | |||||||||||||||||||||||||||||
| 1728 | B<Macro expansion is no respector of quoted strings or anything else> - | ||||||||||||||||||||||||||||
| 1729 | B<if the name matches the expansion will take place!> | ||||||||||||||||||||||||||||
| 1730 | |||||||||||||||||||||||||||||
| 1731 | Multi-line definitions are permitted (here's an example I use with the lout | ||||||||||||||||||||||||||||
| 1732 | typesetting language): | ||||||||||||||||||||||||||||
| 1733 | |||||||||||||||||||||||||||||
| 1734 | %DEFINE SCENE | ||||||||||||||||||||||||||||
| 1735 | @Section | ||||||||||||||||||||||||||||
| 1736 | @Title {#0} | ||||||||||||||||||||||||||||
| 1737 | @Begin | ||||||||||||||||||||||||||||
| 1738 | @PP | ||||||||||||||||||||||||||||
| 1739 | @Include {#1} | ||||||||||||||||||||||||||||
| 1740 | @End @Section | ||||||||||||||||||||||||||||
| 1741 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1742 | |||||||||||||||||||||||||||||
| 1743 | This allows us to write the following in our lout files: | ||||||||||||||||||||||||||||
| 1744 | |||||||||||||||||||||||||||||
| 1745 | SCENE[ The title of the scene | scene1.lt ] | ||||||||||||||||||||||||||||
| 1746 | |||||||||||||||||||||||||||||
| 1747 | which is a lot shorter than the definition. | ||||||||||||||||||||||||||||
| 1748 | |||||||||||||||||||||||||||||
| 1749 | The body of a macro may not contain a literal null. If you really need one | ||||||||||||||||||||||||||||
| 1750 | then use a script and represent the null as C<chr(0)>. | ||||||||||||||||||||||||||||
| 1751 | |||||||||||||||||||||||||||||
| 1752 | B<Converting a macro to a script> | ||||||||||||||||||||||||||||
| 1753 | |||||||||||||||||||||||||||||
| 1754 | This can be achieved very simply. For a one line macro simply enclose the | ||||||||||||||||||||||||||||
| 1755 | body between qq{ and }, e.g. | ||||||||||||||||||||||||||||
| 1756 | |||||||||||||||||||||||||||||
| 1757 | %DEFINE $SURNAME [Baggins] | ||||||||||||||||||||||||||||
| 1758 | |||||||||||||||||||||||||||||
| 1759 | becomes | ||||||||||||||||||||||||||||
| 1760 | |||||||||||||||||||||||||||||
| 1761 | %DEFINE_SCRIPT $SURNAME [qq{Baggins}] | ||||||||||||||||||||||||||||
| 1762 | |||||||||||||||||||||||||||||
| 1763 | For a multi-line macro use a here document, e.g. | ||||||||||||||||||||||||||||
| 1764 | |||||||||||||||||||||||||||||
| 1765 | %DEFINE SCENE | ||||||||||||||||||||||||||||
| 1766 | @Section | ||||||||||||||||||||||||||||
| 1767 | @Title {#0} | ||||||||||||||||||||||||||||
| 1768 | @Begin | ||||||||||||||||||||||||||||
| 1769 | @PP | ||||||||||||||||||||||||||||
| 1770 | @Include {#1} | ||||||||||||||||||||||||||||
| 1771 | @End @Section | ||||||||||||||||||||||||||||
| 1772 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1773 | |||||||||||||||||||||||||||||
| 1774 | becomes | ||||||||||||||||||||||||||||
| 1775 | |||||||||||||||||||||||||||||
| 1776 | %DEFINE_SCRIPT SCENE | ||||||||||||||||||||||||||||
| 1777 | <<__EOT__ | ||||||||||||||||||||||||||||
| 1778 | \@Section | ||||||||||||||||||||||||||||
| 1779 | \@Title {#0} | ||||||||||||||||||||||||||||
| 1780 | \@Begin | ||||||||||||||||||||||||||||
| 1781 | \@PP | ||||||||||||||||||||||||||||
| 1782 | \@Include {#1} | ||||||||||||||||||||||||||||
| 1783 | \@End \@Section | ||||||||||||||||||||||||||||
| 1784 | __EOT__ | ||||||||||||||||||||||||||||
| 1785 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1786 | |||||||||||||||||||||||||||||
| 1787 | Note that the C<@s> had to be escaped because they have a special meaning in | ||||||||||||||||||||||||||||
| 1788 | perl. | ||||||||||||||||||||||||||||
| 1789 | |||||||||||||||||||||||||||||
| 1790 | =head3 %UNDEFINE | ||||||||||||||||||||||||||||
| 1791 | |||||||||||||||||||||||||||||
| 1792 | Macros can be undefined in files: | ||||||||||||||||||||||||||||
| 1793 | |||||||||||||||||||||||||||||
| 1794 | %UNDEFINE *P | ||||||||||||||||||||||||||||
| 1795 | |||||||||||||||||||||||||||||
| 1796 | and in code: | ||||||||||||||||||||||||||||
| 1797 | |||||||||||||||||||||||||||||
| 1798 | $Macro->undefine_macro('*P'); | ||||||||||||||||||||||||||||
| 1799 | |||||||||||||||||||||||||||||
| 1800 | Undefining a non-existing macro is not considered an error. | ||||||||||||||||||||||||||||
| 1801 | |||||||||||||||||||||||||||||
| 1802 | =head3 %UNDEFINE_ALL | ||||||||||||||||||||||||||||
| 1803 | |||||||||||||||||||||||||||||
| 1804 | All macros can be undefined in files: | ||||||||||||||||||||||||||||
| 1805 | |||||||||||||||||||||||||||||
| 1806 | %UNDEFINE_ALL | ||||||||||||||||||||||||||||
| 1807 | |||||||||||||||||||||||||||||
| 1808 | and in code: | ||||||||||||||||||||||||||||
| 1809 | |||||||||||||||||||||||||||||
| 1810 | $Macro->undefine_all_macro; | ||||||||||||||||||||||||||||
| 1811 | |||||||||||||||||||||||||||||
| 1812 | =head3 %DEFINE_SCRIPT | ||||||||||||||||||||||||||||
| 1813 | |||||||||||||||||||||||||||||
| 1814 | Instead of straight textual substitution, we can have some perl executed | ||||||||||||||||||||||||||||
| 1815 | (after any parameters have been replaced in the perl text): | ||||||||||||||||||||||||||||
| 1816 | |||||||||||||||||||||||||||||
| 1817 | %DEFINE_SCRIPT *ADD ["#0 + #1 = " . (#0 + #1)] | ||||||||||||||||||||||||||||
| 1818 | |||||||||||||||||||||||||||||
| 1819 | or by using the equivalent method call: | ||||||||||||||||||||||||||||
| 1820 | |||||||||||||||||||||||||||||
| 1821 | $Macro->define_script( '*ADD', '"#0 + #1 = " . (#0 + #1)' ); | ||||||||||||||||||||||||||||
| 1822 | |||||||||||||||||||||||||||||
| 1823 | We can call our script anything, excluding white-space characters special | ||||||||||||||||||||||||||||
| 1824 | characters used while parsing the input text (C<[,],(,),#>). | ||||||||||||||||||||||||||||
| 1825 | |||||||||||||||||||||||||||||
| 1826 | All names are case-sensitive. | ||||||||||||||||||||||||||||
| 1827 | |||||||||||||||||||||||||||||
| 1828 | These would be used as C<*ADD[5|11]> in the text | ||||||||||||||||||||||||||||
| 1829 | |||||||||||||||||||||||||||||
| 1830 | which would be output as: | ||||||||||||||||||||||||||||
| 1831 | |||||||||||||||||||||||||||||
| 1832 | These would be used as 5 + 11 = 16 in the text | ||||||||||||||||||||||||||||
| 1833 | |||||||||||||||||||||||||||||
| 1834 | In script definitions we can use an alternative way of passing parameters | ||||||||||||||||||||||||||||
| 1835 | instead of or in addition to the C<#0> syntax. | ||||||||||||||||||||||||||||
| 1836 | |||||||||||||||||||||||||||||
| 1837 | This is particularly useful if we want to take a variable number of parameters | ||||||||||||||||||||||||||||
| 1838 | since the C<#0> etc syntax does not provide for this. An array called C<@Param> | ||||||||||||||||||||||||||||
| 1839 | is available to our perl code that has any parameters. This allows things | ||||||||||||||||||||||||||||
| 1840 | like the following to be achieved: | ||||||||||||||||||||||||||||
| 1841 | |||||||||||||||||||||||||||||
| 1842 | %DEFINE_SCRIPT ^PEOPLE | ||||||||||||||||||||||||||||
| 1843 | # We don't use the name hash number params but read straight from the | ||||||||||||||||||||||||||||
| 1844 | # array: | ||||||||||||||||||||||||||||
| 1845 | my $a = "friends and relatives are "; | ||||||||||||||||||||||||||||
| 1846 | $a .= join ", ", @Param; | ||||||||||||||||||||||||||||
| 1847 | $a; | ||||||||||||||||||||||||||||
| 1848 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1849 | |||||||||||||||||||||||||||||
| 1850 | The above would expand in the following text: | ||||||||||||||||||||||||||||
| 1851 | |||||||||||||||||||||||||||||
| 1852 | Her ^PEOPLE[Anna|John|Zebadiah]. | ||||||||||||||||||||||||||||
| 1853 | |||||||||||||||||||||||||||||
| 1854 | to | ||||||||||||||||||||||||||||
| 1855 | |||||||||||||||||||||||||||||
| 1856 | Her friends and relatives are Anna, John, Zebadiah. | ||||||||||||||||||||||||||||
| 1857 | |||||||||||||||||||||||||||||
| 1858 | In addition to having access to the parameters either using the C<#0> syntax or | ||||||||||||||||||||||||||||
| 1859 | the C<@Param> array, we can also access any variables that have been defined | ||||||||||||||||||||||||||||
| 1860 | using L</%DEFINE_VARIABLE>. These are accessible either using | ||||||||||||||||||||||||||||
| 1861 | C<#variablename> similarly to the <#0> parameter syntax, or via the C<%Var> hash. | ||||||||||||||||||||||||||||
| 1862 | Although we can change both C<@Param> and C<%Var> elements in our script, | ||||||||||||||||||||||||||||
| 1863 | the changes to C<@Param> only apply within the script whereas changes to | ||||||||||||||||||||||||||||
| 1864 | C<%Var> apply from that point on globally. | ||||||||||||||||||||||||||||
| 1865 | |||||||||||||||||||||||||||||
| 1866 | Note that if you require a literal C<#> followed by digits in a script body then | ||||||||||||||||||||||||||||
| 1867 | you must escape the C<#> like this C<\#>. | ||||||||||||||||||||||||||||
| 1868 | |||||||||||||||||||||||||||||
| 1869 | Here's a simple date-stamp style: | ||||||||||||||||||||||||||||
| 1870 | |||||||||||||||||||||||||||||
| 1871 | %DEFINE_SCRIPT *DATESTAMP | ||||||||||||||||||||||||||||
| 1872 | use POSIX; | ||||||||||||||||||||||||||||
| 1873 | "#0 on ".strftime("%Y/%m/%d", localtime(time)); | ||||||||||||||||||||||||||||
| 1874 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1875 | |||||||||||||||||||||||||||||
| 1876 | If we wanted to add the above in code we'd have to make sure the | ||||||||||||||||||||||||||||
| 1877 | C<$variables> weren't interpolated: | ||||||||||||||||||||||||||||
| 1878 | |||||||||||||||||||||||||||||
| 1879 | $Macro->define_script( '*DATESTAMP', <<'__EOT__' ); | ||||||||||||||||||||||||||||
| 1880 | use POSIX; | ||||||||||||||||||||||||||||
| 1881 | "#0 on ".strftime("%Y/%m/%d", localtime(time)); | ||||||||||||||||||||||||||||
| 1882 | __EOT__ | ||||||||||||||||||||||||||||
| 1883 | |||||||||||||||||||||||||||||
| 1884 | Here's (a somewhat contrived example of) how the above would be used: | ||||||||||||||||||||||||||||
| 1885 | |||||||||||||||||||||||||||||
| 1886 | <HTML> | ||||||||||||||||||||||||||||
| 1887 | <HEAD><TITLE>Test Page</TITLE></HEAD> | ||||||||||||||||||||||||||||
| 1888 | <BODY> | ||||||||||||||||||||||||||||
| 1889 | *DATESTAMP[Last Updated]<P> | ||||||||||||||||||||||||||||
| 1890 | This page is up-to-date and will remain valid until *DATESTAMP[midnight] | ||||||||||||||||||||||||||||
| 1891 | </BODY> | ||||||||||||||||||||||||||||
| 1892 | </HTML> | ||||||||||||||||||||||||||||
| 1893 | |||||||||||||||||||||||||||||
| 1894 | Thus we could have a file, C<test.html.m> containing: | ||||||||||||||||||||||||||||
| 1895 | |||||||||||||||||||||||||||||
| 1896 | %DEFINE_SCRIPT *DATESTAMP | ||||||||||||||||||||||||||||
| 1897 | use POSIX; | ||||||||||||||||||||||||||||
| 1898 | "#0 on ".strftime("%Y/%m/%d", localtime(time)); | ||||||||||||||||||||||||||||
| 1899 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1900 | <HTML> | ||||||||||||||||||||||||||||
| 1901 | <HEAD><TITLE>Test Page</TITLE></HEAD> | ||||||||||||||||||||||||||||
| 1902 | <BODY> | ||||||||||||||||||||||||||||
| 1903 | *DATESTAMP[Last Updated]<P> | ||||||||||||||||||||||||||||
| 1904 | This page is up-to-date and will remain valid until *DATESTAMP[midnight] | ||||||||||||||||||||||||||||
| 1905 | </BODY> | ||||||||||||||||||||||||||||
| 1906 | </HTML> | ||||||||||||||||||||||||||||
| 1907 | |||||||||||||||||||||||||||||
| 1908 | which when expanded, either in code using C<$Macro-E<gt>expand()>, or using the | ||||||||||||||||||||||||||||
| 1909 | simple C<macropp> utility supplied with C<Text::MacroScript>: | ||||||||||||||||||||||||||||
| 1910 | |||||||||||||||||||||||||||||
| 1911 | % macropp test.html.m > test.html | ||||||||||||||||||||||||||||
| 1912 | |||||||||||||||||||||||||||||
| 1913 | C<test.html> will contain just this: | ||||||||||||||||||||||||||||
| 1914 | |||||||||||||||||||||||||||||
| 1915 | <HTML> | ||||||||||||||||||||||||||||
| 1916 | <HEAD><TITLE>Test Page</TITLE></HEAD> | ||||||||||||||||||||||||||||
| 1917 | <BODY> | ||||||||||||||||||||||||||||
| 1918 | Last Updated on 1999/08/21<P> | ||||||||||||||||||||||||||||
| 1919 | This page is up-to-date and will remain valid until midnight on 1999/08/21 | ||||||||||||||||||||||||||||
| 1920 | </BODY> | ||||||||||||||||||||||||||||
| 1921 | </HTML> | ||||||||||||||||||||||||||||
| 1922 | |||||||||||||||||||||||||||||
| 1923 | Of course in practice we wouldn't want to define everything in-line like this. | ||||||||||||||||||||||||||||
| 1924 | See L</%LOAD> later for an alternative. | ||||||||||||||||||||||||||||
| 1925 | |||||||||||||||||||||||||||||
| 1926 | This example written in embedded style might be written thus: | ||||||||||||||||||||||||||||
| 1927 | |||||||||||||||||||||||||||||
| 1928 | <: | ||||||||||||||||||||||||||||
| 1929 | %DEFINE_SCRIPT DATESTAMP | ||||||||||||||||||||||||||||
| 1930 | use POSIX; | ||||||||||||||||||||||||||||
| 1931 | "#0 on ".strftime("%Y/%m/%d", localtime(time)); | ||||||||||||||||||||||||||||
| 1932 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1933 | :> | ||||||||||||||||||||||||||||
| 1934 | <HTML> | ||||||||||||||||||||||||||||
| 1935 | <HEAD><TITLE>Test Page</TITLE></HEAD> | ||||||||||||||||||||||||||||
| 1936 | <BODY> | ||||||||||||||||||||||||||||
| 1937 | <!-- Note how the parameter must be within the delimiters. --> | ||||||||||||||||||||||||||||
| 1938 | <:DATESTAMP[Last Updated]:><P> | ||||||||||||||||||||||||||||
| 1939 | This page is up-to-date and will remain valid until <:DATESTAMP[midnight]:> | ||||||||||||||||||||||||||||
| 1940 | </BODY> | ||||||||||||||||||||||||||||
| 1941 | </HTML> | ||||||||||||||||||||||||||||
| 1942 | |||||||||||||||||||||||||||||
| 1943 | For more (and better) HTML examples see the example file C<html.macro>. | ||||||||||||||||||||||||||||
| 1944 | |||||||||||||||||||||||||||||
| 1945 | The body of a script may not contain a literal null. If you really need one | ||||||||||||||||||||||||||||
| 1946 | then represent the null as C<chr(0)>. | ||||||||||||||||||||||||||||
| 1947 | |||||||||||||||||||||||||||||
| 1948 | =head3 %UNDEFINE_SCRIPT | ||||||||||||||||||||||||||||
| 1949 | |||||||||||||||||||||||||||||
| 1950 | Scripts can be undefined in files: | ||||||||||||||||||||||||||||
| 1951 | |||||||||||||||||||||||||||||
| 1952 | %UNDEFINE_SCRIPT *DATESTAMP | ||||||||||||||||||||||||||||
| 1953 | |||||||||||||||||||||||||||||
| 1954 | and in code: | ||||||||||||||||||||||||||||
| 1955 | |||||||||||||||||||||||||||||
| 1956 | $Macro->undefine_script('*DATESTAMP'); | ||||||||||||||||||||||||||||
| 1957 | |||||||||||||||||||||||||||||
| 1958 | Undefining a non-existing script is not considered an error. | ||||||||||||||||||||||||||||
| 1959 | |||||||||||||||||||||||||||||
| 1960 | =head3 %UNDEFINE_ALL_SCRIPT | ||||||||||||||||||||||||||||
| 1961 | |||||||||||||||||||||||||||||
| 1962 | All scripts can be undefined in files: | ||||||||||||||||||||||||||||
| 1963 | |||||||||||||||||||||||||||||
| 1964 | %UNDEFINE_ALL_SCRIPT | ||||||||||||||||||||||||||||
| 1965 | |||||||||||||||||||||||||||||
| 1966 | and in code: | ||||||||||||||||||||||||||||
| 1967 | |||||||||||||||||||||||||||||
| 1968 | $Macro->undefine_all_script; | ||||||||||||||||||||||||||||
| 1969 | |||||||||||||||||||||||||||||
| 1970 | =head3 %DEFINE_VARIABLE | ||||||||||||||||||||||||||||
| 1971 | |||||||||||||||||||||||||||||
| 1972 | We can also define variables: | ||||||||||||||||||||||||||||
| 1973 | |||||||||||||||||||||||||||||
| 1974 | %DEFINE_VARIABLE &*! [89.1232] | ||||||||||||||||||||||||||||
| 1975 | |||||||||||||||||||||||||||||
| 1976 | or in code: | ||||||||||||||||||||||||||||
| 1977 | |||||||||||||||||||||||||||||
| 1978 | $Macro->define_variable( '&*!', 89.1232 ); | ||||||||||||||||||||||||||||
| 1979 | |||||||||||||||||||||||||||||
| 1980 | Note that there is no multi-line version of L</%DEFINE_VARIABLE>. | ||||||||||||||||||||||||||||
| 1981 | |||||||||||||||||||||||||||||
| 1982 | All current variables are available inside L</%DEFINE> macros and | ||||||||||||||||||||||||||||
| 1983 | L</%DEFINE_SCRIPT> as C<#varname>. Inside L</%DEFINE_SCRIPT> scripts they | ||||||||||||||||||||||||||||
| 1984 | are also available in the C<%Var> hash: | ||||||||||||||||||||||||||||
| 1985 | |||||||||||||||||||||||||||||
| 1986 | %DEFINE_SCRIPT *TEST1 | ||||||||||||||||||||||||||||
| 1987 | $a = ''; | ||||||||||||||||||||||||||||
| 1988 | while( my( $key, $val ) each( %Var ) ) { | ||||||||||||||||||||||||||||
| 1989 | $a .= "$key = $val\n"; | ||||||||||||||||||||||||||||
| 1990 | } | ||||||||||||||||||||||||||||
| 1991 | $a; | ||||||||||||||||||||||||||||
| 1992 | %END_DEFINE | ||||||||||||||||||||||||||||
| 1993 | |||||||||||||||||||||||||||||
| 1994 | Here's another example: | ||||||||||||||||||||||||||||
| 1995 | |||||||||||||||||||||||||||||
| 1996 | %DEFINE_VARIABLE XCOORD[256] | ||||||||||||||||||||||||||||
| 1997 | %DEFINE_VARIABLE YCOORD[112] | ||||||||||||||||||||||||||||
| 1998 | The X coord is *SCALE[X|16] and the Y coord is *SCALE[Y|16] | ||||||||||||||||||||||||||||
| 1999 | |||||||||||||||||||||||||||||
| 2000 | %DEFINE_SCRIPT *SCALE | ||||||||||||||||||||||||||||
| 2001 | my $coord = shift @Param; | ||||||||||||||||||||||||||||
| 2002 | my $scale = shift @Param; | ||||||||||||||||||||||||||||
| 2003 | my $val = $Var{$coord}; | ||||||||||||||||||||||||||||
| 2004 | $val %= scale; # Scale it | ||||||||||||||||||||||||||||
| 2005 | $val; | ||||||||||||||||||||||||||||
| 2006 | %END_DEFINE | ||||||||||||||||||||||||||||
| 2007 | |||||||||||||||||||||||||||||
| 2008 | Variables can be modified within script L</%DEFINE>s, e.g. | ||||||||||||||||||||||||||||
| 2009 | |||||||||||||||||||||||||||||
| 2010 | %DEFINE_VARIABLE VV[Foxtrot] | ||||||||||||||||||||||||||||
| 2011 | # VV eq 'Foxtrot' | ||||||||||||||||||||||||||||
| 2012 | # other text | ||||||||||||||||||||||||||||
| 2013 | # Here we use the #variable synax: | ||||||||||||||||||||||||||||
| 2014 | %DEFINE_SCRIPT VV[#VV='Alpha'] | ||||||||||||||||||||||||||||
| 2015 | # VV eq 'Alpha' - note that we *must* refer to the script (as we've done | ||||||||||||||||||||||||||||
| 2016 | # on the line following) for it to execute. | ||||||||||||||||||||||||||||
| 2017 | # other text | ||||||||||||||||||||||||||||
| 2018 | # Here we use perl syntax: | ||||||||||||||||||||||||||||
| 2019 | %DEFINE_SCRIPT VV[$Var{'VV'}='Tango'] | ||||||||||||||||||||||||||||
| 2020 | # VV eq 'Tango' - note that we *must* refer to the script (as we've done | ||||||||||||||||||||||||||||
| 2021 | # on the line following) for it to execute. | ||||||||||||||||||||||||||||
| 2022 | |||||||||||||||||||||||||||||
| 2023 | As we can see variables support the C<#variable> syntax similarly to parameters | ||||||||||||||||||||||||||||
| 2024 | which support C<#0> etc and ara available in scripts via the C<@Param> array. | ||||||||||||||||||||||||||||
| 2025 | Note that changing parameters within a script only apply within the script; | ||||||||||||||||||||||||||||
| 2026 | whereas changing variables in the C<%Var> hash in a script changes them from | ||||||||||||||||||||||||||||
| 2027 | that point on globally. | ||||||||||||||||||||||||||||
| 2028 | |||||||||||||||||||||||||||||
| 2029 | Variables are also used with L</%CASE>. | ||||||||||||||||||||||||||||
| 2030 | |||||||||||||||||||||||||||||
| 2031 | =head3 %UNDEFINE_VARIABLE | ||||||||||||||||||||||||||||
| 2032 | |||||||||||||||||||||||||||||
| 2033 | Variables can be undefined in files: | ||||||||||||||||||||||||||||
| 2034 | |||||||||||||||||||||||||||||
| 2035 | %UNDEFINE_VARIABLE &*! | ||||||||||||||||||||||||||||
| 2036 | |||||||||||||||||||||||||||||
| 2037 | and in code: | ||||||||||||||||||||||||||||
| 2038 | |||||||||||||||||||||||||||||
| 2039 | $Macro->undefine_variable('&*!'); | ||||||||||||||||||||||||||||
| 2040 | |||||||||||||||||||||||||||||
| 2041 | Undefining a non-existing variable is not considered an error. | ||||||||||||||||||||||||||||
| 2042 | |||||||||||||||||||||||||||||
| 2043 | =head3 %UNDEFINE_ALL_VARIABLE | ||||||||||||||||||||||||||||
| 2044 | |||||||||||||||||||||||||||||
| 2045 | All variables can be undefined in files: | ||||||||||||||||||||||||||||
| 2046 | |||||||||||||||||||||||||||||
| 2047 | %UNDEFINE_ALL_VARIABLE | ||||||||||||||||||||||||||||
| 2048 | |||||||||||||||||||||||||||||
| 2049 | and in code: | ||||||||||||||||||||||||||||
| 2050 | |||||||||||||||||||||||||||||
| 2051 | $Macro->undefine_all_variable; | ||||||||||||||||||||||||||||
| 2052 | |||||||||||||||||||||||||||||
| 2053 | One use of undefining everything is to ensure we get a clean start. We might | ||||||||||||||||||||||||||||
| 2054 | head up our files thus: | ||||||||||||||||||||||||||||
| 2055 | |||||||||||||||||||||||||||||
| 2056 | %UNDEFINE_ALL | ||||||||||||||||||||||||||||
| 2057 | %UNDEFINE_ALL_SCRIPT | ||||||||||||||||||||||||||||
| 2058 | %UNDEFINE_ALL_VARIABLE | ||||||||||||||||||||||||||||
| 2059 | %LOAD[mymacros] | ||||||||||||||||||||||||||||
| 2060 | text goes here | ||||||||||||||||||||||||||||
| 2061 | |||||||||||||||||||||||||||||
| 2062 | =head2 Loading and including files | ||||||||||||||||||||||||||||
| 2063 | |||||||||||||||||||||||||||||
| 2064 | Although we can define macros directly in the files that require them it is often | ||||||||||||||||||||||||||||
| 2065 | more useful to define them separately and include them in all those that need | ||||||||||||||||||||||||||||
| 2066 | them. | ||||||||||||||||||||||||||||
| 2067 | |||||||||||||||||||||||||||||
| 2068 | One way of achieving this is to load in the macros/scripts first and then | ||||||||||||||||||||||||||||
| 2069 | process the file(s). In code this would be achieved like this: | ||||||||||||||||||||||||||||
| 2070 | |||||||||||||||||||||||||||||
| 2071 | $Macro->load_file( $macro_file ); # loads definitions only | ||||||||||||||||||||||||||||
| 2072 | $Macro->expand_file( $file ); # expands definitions to STDOUT | ||||||||||||||||||||||||||||
| 2073 | my @expanded = $Macro->expand_file( $file ); # expands to array. | ||||||||||||||||||||||||||||
| 2074 | |||||||||||||||||||||||||||||
| 2075 | From the command line it would be achieved thus: | ||||||||||||||||||||||||||||
| 2076 | |||||||||||||||||||||||||||||
| 2077 | % macropp -f html.macros test.html.m > test.html | ||||||||||||||||||||||||||||
| 2078 | |||||||||||||||||||||||||||||
| 2079 | One disadvantage of this approach, especially if we have lots of macro files, | ||||||||||||||||||||||||||||
| 2080 | is that we can easily forget which macro files are required by which text | ||||||||||||||||||||||||||||
| 2081 | files. One solution to this is to go back to C<%DEFINE>ing in the text files | ||||||||||||||||||||||||||||
| 2082 | themselves, but this would lose reusability. The answer to both these problems | ||||||||||||||||||||||||||||
| 2083 | is to use the C<%LOAD> command which loads the definitions from the named file at | ||||||||||||||||||||||||||||
| 2084 | the point it appears in the text file: | ||||||||||||||||||||||||||||
| 2085 | |||||||||||||||||||||||||||||
| 2086 | %LOAD[~/.macro/html.macros] | ||||||||||||||||||||||||||||
| 2087 | <HTML> | ||||||||||||||||||||||||||||
| 2088 | <HEAD><TITLE>Test Page Again</TITLE></HEAD> | ||||||||||||||||||||||||||||
| 2089 | <BODY> | ||||||||||||||||||||||||||||
| 2090 | *DATESTAMP[Last Updated]<P> | ||||||||||||||||||||||||||||
| 2091 | This page will remain valid until *DATESTAMP[midnight] | ||||||||||||||||||||||||||||
| 2092 | </BODY> | ||||||||||||||||||||||||||||
| 2093 | </HTML> | ||||||||||||||||||||||||||||
| 2094 | |||||||||||||||||||||||||||||
| 2095 | The above text has the same output but we don't have to remember or explicitly | ||||||||||||||||||||||||||||
| 2096 | load the macros. In code we can simply do this: | ||||||||||||||||||||||||||||
| 2097 | |||||||||||||||||||||||||||||
| 2098 | my @expanded = $Macro->expand_file( $file ); | ||||||||||||||||||||||||||||
| 2099 | |||||||||||||||||||||||||||||
| 2100 | or from the command line: | ||||||||||||||||||||||||||||
| 2101 | |||||||||||||||||||||||||||||
| 2102 | % macropp test.html.m > test.html | ||||||||||||||||||||||||||||
| 2103 | |||||||||||||||||||||||||||||
| 2104 | At the beginning of our lout typesetting files we might put this line: | ||||||||||||||||||||||||||||
| 2105 | |||||||||||||||||||||||||||||
| 2106 | %LOAD[local.macros] | ||||||||||||||||||||||||||||
| 2107 | |||||||||||||||||||||||||||||
| 2108 | The first line of the C<local.macros> file is: | ||||||||||||||||||||||||||||
| 2109 | |||||||||||||||||||||||||||||
| 2110 | %LOAD[~/.macro/lout.macros] | ||||||||||||||||||||||||||||
| 2111 | |||||||||||||||||||||||||||||
| 2112 | So this loads both global macros then local ones (which if they have the same | ||||||||||||||||||||||||||||
| 2113 | name will of course over-ride). | ||||||||||||||||||||||||||||
| 2114 | |||||||||||||||||||||||||||||
| 2115 | This saves repeating the C<%DEFINE> definitions in all the files and makes | ||||||||||||||||||||||||||||
| 2116 | maintenance easier. | ||||||||||||||||||||||||||||
| 2117 | |||||||||||||||||||||||||||||
| 2118 | C<%LOAD> loads perl scripts and macros, but ignores any other text. Thus we can | ||||||||||||||||||||||||||||
| 2119 | use C<%LOAD>, or its method equivalent C<load_file()>, on I<any> file, and it | ||||||||||||||||||||||||||||
| 2120 | will only ever instantiate macros and scripts and produce no output. When we | ||||||||||||||||||||||||||||
| 2121 | are using embedded processing any file C<%LOAD>ed is treated as if wrapped in | ||||||||||||||||||||||||||||
| 2122 | delimiters. | ||||||||||||||||||||||||||||
| 2123 | |||||||||||||||||||||||||||||
| 2124 | If we want to include the entire contents of another file, and perform macro | ||||||||||||||||||||||||||||
| 2125 | expansion on that file then use C<%INCLUDE>, e.g. | ||||||||||||||||||||||||||||
| 2126 | |||||||||||||||||||||||||||||
| 2127 | %INCLUDE[/path/to/file/with/scripts-and-macros-and-text] | ||||||||||||||||||||||||||||
| 2128 | |||||||||||||||||||||||||||||
| 2129 | The C<%INCLUDE> command will instantiate any macros and scripts it encounters | ||||||||||||||||||||||||||||
| 2130 | and include all other lines of text (with macro/script expansion) in the | ||||||||||||||||||||||||||||
| 2131 | output stream. | ||||||||||||||||||||||||||||
| 2132 | |||||||||||||||||||||||||||||
| 2133 | Macros and scripts are expanded in the following order: | ||||||||||||||||||||||||||||
| 2134 | 1. scripts (longest named first, shortest named last) | ||||||||||||||||||||||||||||
| 2135 | 2. macros (longest named first, shortest named last) | ||||||||||||||||||||||||||||
| 2136 | |||||||||||||||||||||||||||||
| 2137 | =head3 %LOAD | ||||||||||||||||||||||||||||
| 2138 | |||||||||||||||||||||||||||||
| 2139 | %LOAD[file] | ||||||||||||||||||||||||||||
| 2140 | |||||||||||||||||||||||||||||
| 2141 | or its code equivalent | ||||||||||||||||||||||||||||
| 2142 | |||||||||||||||||||||||||||||
| 2143 | $Macro->load_file( $filename ); | ||||||||||||||||||||||||||||
| 2144 | |||||||||||||||||||||||||||||
| 2145 | instatiates any definitions that appear in the file, but ignores any other text | ||||||||||||||||||||||||||||
| 2146 | and produces no output. When we are using embedded processing any file | ||||||||||||||||||||||||||||
| 2147 | L</%LOAD>ed is treated as if wrapped in delimiters. | ||||||||||||||||||||||||||||
| 2148 | |||||||||||||||||||||||||||||
| 2149 | This is equivalent to calling C<macropp -f>. | ||||||||||||||||||||||||||||
| 2150 | |||||||||||||||||||||||||||||
| 2151 | New defintions of the same macro override old defintions, thus one can first | ||||||||||||||||||||||||||||
| 2152 | L</%LOAD> a global macro file, and then a local project file that can override | ||||||||||||||||||||||||||||
| 2153 | some of the global macros. | ||||||||||||||||||||||||||||
| 2154 | |||||||||||||||||||||||||||||
| 2155 | =head3 %INCLUDE | ||||||||||||||||||||||||||||
| 2156 | |||||||||||||||||||||||||||||
| 2157 | %INCLUDE[file] | ||||||||||||||||||||||||||||
| 2158 | |||||||||||||||||||||||||||||
| 2159 | or its code equivalent | ||||||||||||||||||||||||||||
| 2160 | |||||||||||||||||||||||||||||
| 2161 | $Macro->expand_file( $filename ); | ||||||||||||||||||||||||||||
| 2162 | |||||||||||||||||||||||||||||
| 2163 | instatiates any definitions that appear in the file, expands definitions | ||||||||||||||||||||||||||||
| 2164 | and sends any other text to the current output filehandle. | ||||||||||||||||||||||||||||
| 2165 | |||||||||||||||||||||||||||||
| 2166 | =head3 %REQUIRE | ||||||||||||||||||||||||||||
| 2167 | |||||||||||||||||||||||||||||
| 2168 | We often want our scripts to have access to a bundle of functions that we have | ||||||||||||||||||||||||||||
| 2169 | created or that are in other modules. This can now be achieved by: | ||||||||||||||||||||||||||||
| 2170 | |||||||||||||||||||||||||||||
| 2171 | %REQUIRE[/path/to/mylibrary.pl] | ||||||||||||||||||||||||||||
| 2172 | |||||||||||||||||||||||||||||
| 2173 | An example library C<macroutil.pl> is provided with examples of usage in | ||||||||||||||||||||||||||||
| 2174 | C<html.macro>. | ||||||||||||||||||||||||||||
| 2175 | |||||||||||||||||||||||||||||
| 2176 | There is no equivalent object method because if we're writing code we can | ||||||||||||||||||||||||||||
| 2177 | C<use> or c<require> as needed and if we're writing macros then we use | ||||||||||||||||||||||||||||
| 2178 | L</%REQUIRE>. | ||||||||||||||||||||||||||||
| 2179 | |||||||||||||||||||||||||||||
| 2180 | =head2 Control Structures | ||||||||||||||||||||||||||||
| 2181 | |||||||||||||||||||||||||||||
| 2182 | =head3 %CASE | ||||||||||||||||||||||||||||
| 2183 | |||||||||||||||||||||||||||||
| 2184 | It is possible to selectively skip parts of the text. | ||||||||||||||||||||||||||||
| 2185 | |||||||||||||||||||||||||||||
| 2186 | %CASE[0] | ||||||||||||||||||||||||||||
| 2187 | All the text here will be discarded. | ||||||||||||||||||||||||||||
| 2188 | No matter how much there is. | ||||||||||||||||||||||||||||
| 2189 | This is effectively a `comment' case. | ||||||||||||||||||||||||||||
| 2190 | %END_CASE | ||||||||||||||||||||||||||||
| 2191 | |||||||||||||||||||||||||||||
| 2192 | The above is useful for multi-line comments. | ||||||||||||||||||||||||||||
| 2193 | |||||||||||||||||||||||||||||
| 2194 | We can also skip selectively. Here's an if...then: | ||||||||||||||||||||||||||||
| 2195 | |||||||||||||||||||||||||||||
| 2196 | %CASE[#OS eq 'Linux'] | ||||||||||||||||||||||||||||
| 2197 | Skipped if the condition is FALSE. | ||||||||||||||||||||||||||||
| 2198 | %END_CASE | ||||||||||||||||||||||||||||
| 2199 | |||||||||||||||||||||||||||||
| 2200 | The condition can be any perl fragment. We can use previously defined | ||||||||||||||||||||||||||||
| 2201 | variables either using the C<#variable> syntax as shown above or using the | ||||||||||||||||||||||||||||
| 2202 | exported perl name, thus in this case either C<#OS>, or C<%Var{'OS'}> | ||||||||||||||||||||||||||||
| 2203 | whichever we prefer. | ||||||||||||||||||||||||||||
| 2204 | |||||||||||||||||||||||||||||
| 2205 | If the condition is true the text is output with macro/script expansion as | ||||||||||||||||||||||||||||
| 2206 | normal; if the condition is false the text is skipped. | ||||||||||||||||||||||||||||
| 2207 | |||||||||||||||||||||||||||||
| 2208 | The if...then...else structure: | ||||||||||||||||||||||||||||
| 2209 | |||||||||||||||||||||||||||||
| 2210 | %DEFINE_VARIABLE OS[Linux] | ||||||||||||||||||||||||||||
| 2211 | |||||||||||||||||||||||||||||
| 2212 | %CASE[$Var{'OS'} eq 'Linux'] | ||||||||||||||||||||||||||||
| 2213 | Linux specific stuff. | ||||||||||||||||||||||||||||
| 2214 | %CASE[#OS ne 'Linux'] | ||||||||||||||||||||||||||||
| 2215 | Non-linux stuff - note that both references to the OS variable are | ||||||||||||||||||||||||||||
| 2216 | identical in the expression (#OS is converted internally to $Var{'0S'} so | ||||||||||||||||||||||||||||
| 2217 | the eval sees the same code in both cases | ||||||||||||||||||||||||||||
| 2218 | %END_CASE | ||||||||||||||||||||||||||||
| 2219 | |||||||||||||||||||||||||||||
| 2220 | Although nested L</%CASE>s are not supported we can get the same functionality | ||||||||||||||||||||||||||||
| 2221 | (and indeed more versatility because we can use full perl expressions), e.g.: | ||||||||||||||||||||||||||||
| 2222 | |||||||||||||||||||||||||||||
| 2223 | %DEFINE_VARIABLE TARGET[Linux] | ||||||||||||||||||||||||||||
| 2224 | |||||||||||||||||||||||||||||
| 2225 | %CASE[#TARGET eq 'Win32' or #TARGET eq 'DOS'] | ||||||||||||||||||||||||||||
| 2226 | Win32/DOS stuff. | ||||||||||||||||||||||||||||
| 2227 | %CASE[#TARGET eq 'Win32'] | ||||||||||||||||||||||||||||
| 2228 | Win32 only stuff. | ||||||||||||||||||||||||||||
| 2229 | %CASE[#TARGET eq 'DOS'] | ||||||||||||||||||||||||||||
| 2230 | DOS only stuff. | ||||||||||||||||||||||||||||
| 2231 | %CASE[#TARGET eq 'Win32' or #TARGET eq 'DOS'] | ||||||||||||||||||||||||||||
| 2232 | More Win32/DOS stuff. | ||||||||||||||||||||||||||||
| 2233 | %END_CASE | ||||||||||||||||||||||||||||
| 2234 | |||||||||||||||||||||||||||||
| 2235 | Although C<macropp> doesn't support nested L</%CASE>'s we can still represent | ||||||||||||||||||||||||||||
| 2236 | logic like this: | ||||||||||||||||||||||||||||
| 2237 | |||||||||||||||||||||||||||||
| 2238 | if cond1 then | ||||||||||||||||||||||||||||
| 2239 | if cond2 | ||||||||||||||||||||||||||||
| 2240 | do cond1 + cond2 stuff | ||||||||||||||||||||||||||||
| 2241 | else | ||||||||||||||||||||||||||||
| 2242 | do cond1 stuff | ||||||||||||||||||||||||||||
| 2243 | end if | ||||||||||||||||||||||||||||
| 2244 | else | ||||||||||||||||||||||||||||
| 2245 | do other stuff | ||||||||||||||||||||||||||||
| 2246 | end if | ||||||||||||||||||||||||||||
| 2247 | |||||||||||||||||||||||||||||
| 2248 | By `unrolling' the expression and writing something like this: | ||||||||||||||||||||||||||||
| 2249 | |||||||||||||||||||||||||||||
| 2250 | %CASE[#cond1 and #cond2] | ||||||||||||||||||||||||||||
| 2251 | do cond1 + cond2 stuff | ||||||||||||||||||||||||||||
| 2252 | %CASE[#cond1 and (not #cond2)] | ||||||||||||||||||||||||||||
| 2253 | do cond1 stuff | ||||||||||||||||||||||||||||
| 2254 | %CASE[(not #cond1) and (not #cond2)] | ||||||||||||||||||||||||||||
| 2255 | do other stuff | ||||||||||||||||||||||||||||
| 2256 | %END_CASE | ||||||||||||||||||||||||||||
| 2257 | |||||||||||||||||||||||||||||
| 2258 | In other words we must fully specify the conditions for each case. | ||||||||||||||||||||||||||||
| 2259 | |||||||||||||||||||||||||||||
| 2260 | We can use any other macro/script command within L</%CASE> commands, e.g. | ||||||||||||||||||||||||||||
| 2261 | L</%DEFINE>s, etc., as well as have any text that will be macro/script expanded | ||||||||||||||||||||||||||||
| 2262 | as normal. | ||||||||||||||||||||||||||||
| 2263 | |||||||||||||||||||||||||||||
| 2264 | =head2 Comments | ||||||||||||||||||||||||||||
| 2265 | |||||||||||||||||||||||||||||
| 2266 | Generally the text files that we process are in formats that support | ||||||||||||||||||||||||||||
| 2267 | commenting, e.g. HTML: | ||||||||||||||||||||||||||||
| 2268 | |||||||||||||||||||||||||||||
| 2269 | <!-- This is an HTML comment --> | ||||||||||||||||||||||||||||
| 2270 | |||||||||||||||||||||||||||||
| 2271 | Sometimes however we want to put comments in our macro source files that won't | ||||||||||||||||||||||||||||
| 2272 | end up in the output files. One simple way of achieving this is to define a | ||||||||||||||||||||||||||||
| 2273 | macro whose body is empty; when its called with any number of parameters (our | ||||||||||||||||||||||||||||
| 2274 | comments), their text is thrown away: | ||||||||||||||||||||||||||||
| 2275 | |||||||||||||||||||||||||||||
| 2276 | %DEFINE %%[] | ||||||||||||||||||||||||||||
| 2277 | |||||||||||||||||||||||||||||
| 2278 | which is used like this in texts: | ||||||||||||||||||||||||||||
| 2279 | |||||||||||||||||||||||||||||
| 2280 | The comment comes %%[Here | [anything] put here will disappear]here! | ||||||||||||||||||||||||||||
| 2281 | |||||||||||||||||||||||||||||
| 2282 | The output of the above will be: | ||||||||||||||||||||||||||||
| 2283 | |||||||||||||||||||||||||||||
| 2284 | The comment comes here! | ||||||||||||||||||||||||||||
| 2285 | |||||||||||||||||||||||||||||
| 2286 | We can add the definition in code: | ||||||||||||||||||||||||||||
| 2287 | |||||||||||||||||||||||||||||
| 2288 | $Macro->define( -macro, '%%', '' ); | ||||||||||||||||||||||||||||
| 2289 | |||||||||||||||||||||||||||||
| 2290 | Or the macro can be added automatically for us when we create the Macro | ||||||||||||||||||||||||||||
| 2291 | object: | ||||||||||||||||||||||||||||
| 2292 | |||||||||||||||||||||||||||||
| 2293 | my $Macro = Text::MacroScript->new( -comment => 1 ); | ||||||||||||||||||||||||||||
| 2294 | # All other options may be used too of course. | ||||||||||||||||||||||||||||
| 2295 | |||||||||||||||||||||||||||||
| 2296 | However the easiest way to comment is to use L</%CASE>: | ||||||||||||||||||||||||||||
| 2297 | |||||||||||||||||||||||||||||
| 2298 | %CASE[0] | ||||||||||||||||||||||||||||
| 2299 | This unconditionally skips text up until the end marker since the | ||||||||||||||||||||||||||||
| 2300 | condition is always false. | ||||||||||||||||||||||||||||
| 2301 | %END_CASE | ||||||||||||||||||||||||||||
| 2302 | |||||||||||||||||||||||||||||
| 2303 | =head1 IMPORTABLE FUNCTIONS | ||||||||||||||||||||||||||||
| 2304 | |||||||||||||||||||||||||||||
| 2305 | In version 1.25 I introduced some useful importable functions. These have now | ||||||||||||||||||||||||||||
| 2306 | been removed from the module. Instead I supply a perl library C<macroutil.pl> | ||||||||||||||||||||||||||||
| 2307 | which has these functions (abspath, relpath, today) since Text::MacroScript | ||||||||||||||||||||||||||||
| 2308 | can now `require' in any library file you like using the L</%REQUIRE> | ||||||||||||||||||||||||||||
| 2309 | directive. | ||||||||||||||||||||||||||||
| 2310 | |||||||||||||||||||||||||||||
| 2311 | =head1 EXAMPLES | ||||||||||||||||||||||||||||
| 2312 | |||||||||||||||||||||||||||||
| 2313 | I now include a sample C<html.macro> file for use with HTML documents. It uses | ||||||||||||||||||||||||||||
| 2314 | the C<macrodir> program (supplied). The macro examples include macros which | ||||||||||||||||||||||||||||
| 2315 | use C<relpath> and also two macros which will include `new' and `updated' | ||||||||||||||||||||||||||||
| 2316 | images up until a specified expiry date using variables. | ||||||||||||||||||||||||||||
| 2317 | |||||||||||||||||||||||||||||
| 2318 | (Also see DESCRIPTION.) | ||||||||||||||||||||||||||||
| 2319 | |||||||||||||||||||||||||||||
| 2320 | =head1 BUGS | ||||||||||||||||||||||||||||
| 2321 | |||||||||||||||||||||||||||||
| 2322 | Lousy error reporting for embedded perl in most cases. | ||||||||||||||||||||||||||||
| 2323 | |||||||||||||||||||||||||||||
| 2324 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 2325 | |||||||||||||||||||||||||||||
| 2326 | Mark Summerfield. I can be contacted as <summer@perlpress.com> - | ||||||||||||||||||||||||||||
| 2327 | please include the word 'macro' in the subject line. | ||||||||||||||||||||||||||||
| 2328 | |||||||||||||||||||||||||||||
| 2329 | =head1 MAINTAINER | ||||||||||||||||||||||||||||
| 2330 | |||||||||||||||||||||||||||||
| 2331 | Since 2015, Paulo Custodio. | ||||||||||||||||||||||||||||
| 2332 | |||||||||||||||||||||||||||||
| 2333 | This module repository is kept in Github, please feel free to submit issues, | ||||||||||||||||||||||||||||
| 2334 | fork and send pull requests. | ||||||||||||||||||||||||||||
| 2335 | |||||||||||||||||||||||||||||
| 2336 | https://github.com/pauloscustodio/Text-MacroScript | ||||||||||||||||||||||||||||
| 2337 | |||||||||||||||||||||||||||||
| 2338 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 2339 | |||||||||||||||||||||||||||||
| 2340 | Copyright (c) Mark Summerfield 1999-2000. All Rights Reserved. | ||||||||||||||||||||||||||||
| 2341 | |||||||||||||||||||||||||||||
| 2342 | Copyright (c) Paulo Custodio 2015. All Rights Reserved. | ||||||||||||||||||||||||||||
| 2343 | |||||||||||||||||||||||||||||
| 2344 | This module may be used/distributed/modified under the LGPL. | ||||||||||||||||||||||||||||
| 2345 | |||||||||||||||||||||||||||||
| 2346 | =cut | ||||||||||||||||||||||||||||