# JAM22 TRACKS # Copyright (c) 2004 Denis Petrov # $Id: magic.pl,v 1.3 2004/04/26 00:25:34 Owner Exp $ # Distributed under the terms of the GNU General Public License # # Template Processor use strict; use Data::Dumper; package main; sub Magic { my %labels; my @parsed = $_[0] =~ /(<%.+?(?=%>)%>)(.*?)(?=<%|$)/gs; # print Dumper(\@parsed); my $i = 0; my $len = scalar(@parsed); for ( $i = 0; $i < $len; $i++ ) { my $l = $parsed[$i]; if ( $l =~ /<%(\w+)%>/ ) { $labels{$1} = $i; } elsif ( $l =~ /<%(.+)%>/ ) { my $result = eval($1); if ( $result =~ /\w+/ ) { if ( exists $labels{$result} ) { $i = $labels{$result}; } else { #trace labels forward my $j; for ( $j = $i+1; $j < $len; $j++ ) { if ( $parsed[$j] =~ /<%(\w+)%>/ ) { $labels{$1} = $j; if ( $1 eq $result ) { $i = $j; last; } } } } } } else { $l =~ s/\"/\\\"/g; $Q->print( eval("\"".$l."\"") ); } } } 1;