
PerlKid
stranger
Jul 3, 2001, 9:21 PM
Post #5 of 5
(15816 views)
|
UPDATE: after testing the provided suggestion further I realised that didn't work either. I have got it working finally, though. It seems that I need to group the whole block of code in addition to the parameters.... and I'm also using a while loop instead of an anonymous sub and an re to check for more tags. The complete working code I have now is:
package CamelSoup::Template; use strict; use vars qw($tag); sub new { my ($class, %tags) = @_; ($tag->{$_} = $tags{$_}) foreach (keys %tags); return bless $tag, $class; } sub parse { my ($tag, $file, $path, $temp) = (@_, ''); open (FILE, "$path/$file") or return ("Can't find file $path/$file: $!"); read FILE, $temp, 1024; close (FILE); while ($temp =~ /(<%include\s(.+?)%>)/) { my ($block, $include, $out) = ($1, $2, ''); open (INC, "$path/$include") or return ("Can't find file $path/$file: $!"); read INC, $out, 1024; close (INC); $temp =~ s/$block/$out/e; } while ($temp =~ /(<%set\s(\w+)%>(.+?)<%endset%>)/) { my ($block, $var, $out) = ($1, $2, $3); $tag->{$var} = $out; $temp =~ s/$block/''/e; } while ($temp =~ /(<%if\s(\w+)\s?([^\s]+)?\s?(\w+)?%>(.+?)<%endif%>)/s) { my ($block, $var, $op, $val, $out, $show) = ($1, $2, $3, $4, $5, ''); if ($op) { if ($op eq 'eq') { ($tag->{$var} eq $val) and $show++; } elsif ($op eq 'ne') { ($tag->{$var} ne $val) and $show++; } elsif ($op eq 'gt') { ($tag->{$var} gt $val) and $show++; } elsif ($op eq 'lt') { ($tag->{$var} lt $val) and $show++; } elsif ($op eq 'ge') { ($tag->{$var} ge $val) and $show++; } elsif ($op eq 'le') { ($tag->{$var} le $val) and $show++; } elsif ($op eq '==') { ($tag->{$var} == $val) and $show++; } elsif ($op eq '!=') { ($tag->{$var} != $val) and $show++; } elsif ($op eq '>') { ($tag->{$var} > $val) and $show++; } elsif ($op eq '<') { ($tag->{$var} < $val) and $show++; } elsif ($op eq '>=') { ($tag->{$var} >= $val) and $show++; } elsif ($op eq '<=') { ($tag->{$var} <= $val) and $show++; } elsif ($op eq 'and') { ($tag->{$var} and $tag->{$val}) and $show++; } elsif ($op eq 'or') { ($tag->{$var} or $tag->{$val}) and $show++; } elsif ($op eq '=~') { ($tag->{$var} =~ $val) and $show++; } elsif ($op eq '!~') { ($tag->{$var} !~ $val) and $show++; } else { ($out = "malformed condition: 'if $var $op $val'") and $show++; } } else { defined $tag->{$var} and $show++; } $temp =~ s/$block/$out if $show/e; } while ($temp =~ /(<%(\w+)%>)/) { my ($block, $var, $out) = ($1, $2, ''); (defined $tag->{$var}) ? ($out = $tag->{$var}) : ($out = "unknown variable: '$var'"); $temp =~ s/$block/$out/e; } return $temp; } sub strip_blanks { return join "\n", grep { !/^\s*$/ } split /\n/, pop; } 1; comments or suggestions would be appreciated. --Drew http://www.camelsoup.com
s;[\d\$&(\^)];;g+s;\.; ;g+s;(.)(..);$2$1;g+print,if$_='&61k4I.)l6il.edn7(K2e^ny$';
|