#!/usr/local/bin/perl

use strict ;
use warnings ;

use Template::Simple ;
use Benchmark qw( timethese ) ;
use Data::Dumper ;

my @dims = @ARGV ? @ARGV : ( 3, 6 ) ;

my $data = generate_data( @dims ) ;
#print Dumper $data ;

my $tmpl = Template::Simple->new() ;

my $template = <<TMPL ;
Header
Val 1: [%val_1%]
[%START nest%]
	Level: [%level%]
[%END nest%]
[%START opt%]
	Optional Level: [%level%]
[%END opt%]
Val 2: [%val_2%]
Footer
TMPL

$tmpl->add_templates( { bench => $template } ) ;

my $x = <<XXX ;
XXX

my $rendered = $tmpl->render( 'bench', $data ) ;

#$tmpl->compile( 'bench', $data ) ;
#print $tmpl->get_source( 'bench' ) ;
#my $comp_rendered = $tmpl->render( 'bench', $data ) ;
#my $ok = $$rendered eq $$comp_rendered ;

my $joined = joined( $data ) ;
my $appended = joined( $data ) ;

my $ok = $$rendered eq $$appended ;

print "OK [$ok]\n" ;

# print $$rendered ;
# print "----\n" ;
# print $$joined ;
#print $$comp_rendered ;

timethese( shift || -2,
	{
		render	=> sub { my $x = $tmpl->render( 'bench', $data ) },
		joined	=> sub { my $x = joined( $data ) },
		concat	=> sub { my $x = concat( $data ) },
		append	=> sub { my $x = append( $data ) },
	}
) ;

exit ;

no warnings ;


sub rendered {

}

sub joined {

no warnings ;

	my( $data ) = @_ ;

	my $out = do {
		my $out ;

		my @data = $data ;
		while( defined( my $data = shift @data ) ) {

			if ( ref $data eq 'ARRAY' ) {
				push @data, @{$data} ;
				next ;
			}

			$out .= ref $data ne 'HASH' ? $data :

			join( '',
			"Header\nVal 1: "
			,
			$data->{val_1}
			,
			"\n"
			,
			do {
				my $data = $data->{nest} ;
				my $out ;

				my @data = $data ;
				while( defined( my $data = shift @data ) ) {

					if ( ref $data eq 'ARRAY' ) {
						push @data, @{$data} ;
						next ;
					}

					$out .= ref $data ne 'HASH' ? $data :
					join( '',
					"\n\tLevel: "
					,
					$data->{level}
					,
					"\n" ) ;
				}
				$out ;
			}
			,
			"\n"
			,
			do {
				my $data = $data->{opt} ;
				my $out ;

				my @data = $data ;
				while( defined( my $data = shift @data ) ) {

					if ( ref $data eq 'ARRAY' ) {
						push @data, @{$data} ;
						next ;
					}

					$out .= ref $data ne 'HASH' ? $data :
					join( '', 
					"\n\tOptional Level: "
					,
					$data->{level}
					,
					"\n" ) ;
				}
				$out ;
			}
			,
			"\nVal 2: "
			,
			$data->{val_2}
			,
			"\nFooter\n" ) ;
		}
		$out ;
	} ;

	return \$out ;
}


sub append {

}


sub concat {

no warnings ;

	my( $data ) = @_ ;

	my $out = do {
		my $out ;

		my @data = $data ;
		while( defined( my $data = shift @data ) ) {

			if ( ref $data eq 'ARRAY' ) {
				push @data, @{$data} ;
				next ;
			}

			$out .= ref $data ne 'HASH' ? $data :
			"Header\nVal 1: "
			.
			$data->{val_1}
			.
			"\n"
			.
			do {
				my $data = $data->{nest} ;
				my $out ;

				my @data = $data ;
				while( defined( my $data = shift @data ) ) {

					if ( ref $data eq 'ARRAY' ) {
						push @data, @{$data} ;
						next ;
					}

					$out .= ref $data ne 'HASH' ? $data :
					"\n\tLevel: "
					.
					$data->{level}
					.
					"\n" ;
				}
				$out ;
			}
			.
			"\n"
			.
			do {
				my $data = $data->{opt} ;
				my $out ;

				my @data = $data ;
				while( defined( my $data = shift @data ) ) {

					if ( ref $data eq 'ARRAY' ) {
						push @data, @{$data} ;
						next ;
					}

					$out .= ref $data ne 'HASH' ? $data :
					"\n\tOptional Level: "
					.
					$data->{level}
					.
					"\n" ;
				}
				$out ;
			}
			.
			"\nVal 2: "
			.
			$data->{val_2}
			.
			"\nFooter\n" ;
		}
		$out ;
	} ;

	return \$out ;
}

sub append {

no warnings ;

	my( $data ) = @_ ;

	my $out ;

	my @data = $data ;
	while( defined( my $data = shift @data ) ) {

		if ( ref $data eq 'ARRAY' ) {
			push @data, @{$data} ;
			next ;
		}

		$out .= ref $data ne 'HASH' ? $data :
		"Header\nVal 1: "
		.
		$data->{val_1}
		.
		"\n" ;

		my @data = $data->{nest} ;
		while( defined( my $data = shift @data ) ) {

			if ( ref $data eq 'ARRAY' ) {
				push @data, @{$data} ;
				next ;
			}

			$out .= ref $data ne 'HASH' ? $data :
			"\n\tLevel: "
			.
			$data->{level}
			.
			"\n" ;
		}

		$out .=
		"\n" ;

		my @data = $data->{opt} ;
		while( defined( my $data = shift @data ) ) {

			if ( ref $data eq 'ARRAY' ) {
				push @data, @{$data} ;
				next ;
			}

			$out .= ref $data ne 'HASH' ? $data :
			"\n\tOptional Level: "
			.
			$data->{level}
			.
			"\n" ;
		}

		$out .=
		"\nVal 2: "
		.
		$data->{val_2}
		.
		"\nFooter\n" ;
	}

	return \$out ;
}


sub generate_data {

	my( $outer_size, $inner_size ) = @_ ;

	my @outer_data ;

	for my $outer ( 1 .. $outer_size ) {

		my @inner_data ;

		for my $inner ( 1 .. $inner_size ) {

			push @inner_data, {

				val_1	=> ($outer x 2),
				val_2	=> ($inner x 3),
				nest	=> {
					level => "$outer-$inner",
				},
				$inner % 2 ? () : (
			
					opt	=> {
						level => "$outer-$inner",
					},
				),
			} ;
		}

		push @outer_data, [@inner_data] ;
	}

	return \@outer_data ;
}
