#!/usr/bin/perl

my($string,$stringnew,$txt,$zpgm,$zkey,%hash);
local($zii);
$hash{aaa}='www';
$hash{bbb}='xxx';
$hash{ccc}='zzz';

$zii=0;
$string='<%zzz%>original text <%=aaa%> value</%zzz%>
          some filler stuff <%xxx%>2nd orig <%=bbb%> txt  
         </%xxx%> and later
         <%zzz%>2nd original text <%=ccc%> value</%zzz%>';

print "\norig string='$string\n";

foreach $zpgm ($string =~ /<%.*?%>/g)
{ 
  $zpgm=substr($zpgm,2,length($zpgm)-4);
  next if(substr($zpgm,0,1) eq '=');

  $string =~ s|<%$zpgm%>(.*?)</%$zpgm%>|&$zpgm($1,%hash);|sge;
  print "zpgm='$zpgm' used\n";
  print "string='$string'\n";

}
exit;

sub www
{ my($xtemp,%xhash)=@_;
  print "\nwww xtemp='$xtemp'\n";
  return(&zzz($xtemp,%xhash));
}

sub xxx
{ my($xtemp,%xhash)=@_;
  print "\nxxx xtemp='$xtemp'\n";
  return(&zzz($xtemp,%xhash));
}

sub zzz
{ my($xtemp,%xhash)=@_;
  my($zkey,$zval);
  $zii++;
  print "zzz $zii xtemp='$xtemp'\n";
  foreach $zkey($xtemp =~ /<%=.*?%>/g)
  { $zkey=substr($zkey,3,length($zkey)-5);
    $zval=$xhash{$zkey};
    $xtemp =~ s/<%=$zkey%>/$zval/g;
  }
  return($xtemp);
}



    foreach $zkey ($zrec =~ /<%=.*?%>/g)
    { $zkey=substr($zkey,3,length($zkey)-5);
      $zval=$xmrg{$zkey};
      if($zval eq '')
      { $zrec =~ s/"<%=$zkey%>"/"$zval"/g;
        $zrec =~ s/<%=$zkey%>/$zval2/g;
      }
      else
      { if($zrec =~ /"<%=$zkey%>"/
         ||$zrec =~ /><%=$zkey%></)
        { $zval =~ s/<br>/\n/ig;
        }
        $zrec =~ s/<%=$zkey%>/$zval/g;
      }
    }

