#fmtsub - perform substitutions # Constants sub fmt { local(@data); local($tmpl) = shift; if ($tmpl) { local(@tmpl) = @$tmpl } local($data) = shift; if ( $data ) { @data = @$data } local($rslt)=""; local(@rlst)=(); local($look_keywd)=1; local($n_key)=0; local($keywd,$line,$value); undef(%key); foreach (@data) { # # look for lines of the form :key: # if ($look_keywd) { if(/:(\w+):/) { $keywd=$1; $look_keywd=0; s/:\w+://; s/^[ \t]*//; s/[ \t]*$//; s/^\n$//; $line=$_; next; } } else { if(/:(\w+):/) { $look_keywd=1; chop($line); $key{$keywd} = $line; redo; } $line=$line . $_; } } # last keyword chop($line); $key{$keywd} = $line; foreach (@tmpl) { if (/#/) { while(1) { last if(! /#([^#]+)#/); if (defined ($value=$key{$1})) { s/#$1#/$value/e; next; } elsif ( s/^([^#]*)#\w+#/\1/ ) { next; } else { last if (! /(.*#.*)(#.*#.*)/); $rslt .= $1; $_ =$2 . "\n"; } } } $rslt .= $_; push(@rslt,$_); } return wantarray ? @rslt : $rslt; } 1;