# -------------------------------------------------------------------------------------
# lib::Template::Perl
# -------------------------------------------------------------------------------------
#
#    Author   : Jean-Michel Hiver                         (c) Jean-Michel Hiver, 2001.
#    Synopsis : Provides the implementation for the
#    <% perl ... %> tag
# -------------------------------------------------------------------------------------
package lib::Template::Perl;
use lib::Template::AbstractTag;
use lib::Exception;
use lib::Template;
use strict;
use vars qw /@ISA/;
@ISA = qw /lib::Template::AbstractTag/;


##
# $obj->main;
# -----------
#   Executes the perl code which is contained within the <% perl ... %> tag.
#   Inside this perl code, there are two special variables:
#
#   $LOCAL  is a hashref that represent the current scope hashref
#   $GLOBAL is a hashref that represent the global variable hashref
#
#   Note: nothing should be directly printed, but put in either $LOCAL or
#         $GLOBAL in order to be displayed by "presentation" tags.
## 
sub main
{
    my $self = shift;
    my $result = $self->result;
    my $tokens = $self->tokens;
    
    my $perl_tag = shift (@{$tokens});
    my $r_token = $lib::Template::RIGHT_TOKEN;
    my $l_token = $lib::Template::LEFT_TOKEN;
    $perl_tag =~ s/^$l_token\s*perl//;
    $perl_tag =~ s/$r_token$//;
    
    my $GLOBAL = $lib::Template::STASH;
    my $LOCAL  = $self->{hash};
    eval "$perl_tag";
    push @{$result}, '[ ' . $@ . ' ]' if $@;
}


1;
