#
# Example for using Embperl::Execute
#
# run this under mod_perl / Apache::Registry
#
use Embperl ;
my($r) = @_;
# workaround for broken $r -> chdir_file in Apache::Registry on ActiveState perl
use Cwd ;
use File::Basename ;
if ($Embperl::modperlapi < 2)
    {
    eval { require Apache::compat } ;
    $@ = '' ;
    }
else
    {
    eval { require Apache2::compat } ;
    $@ = '' ;
    }
    
my $fn = $r -> filename ;
chdir(dirname ($fn)) ;
$Embperl::DebugDefault = 0xffffdffd ;
$tst1 = '
Here is some text
' ;
$r -> status (200) ;
$r -> send_http_header () ;
print "Test for Embperl::Execute\n" ;
print " 1.) Include from memory
\n" ;
Embperl::Execute ({input		=> \$tst1,
						 mtime      => 1,  
						 inputfile	=> 'Some text',
						 req_rec    => $r}) ;
print "\n 2.) Include from memory with some Embperl code
\n" ;
Embperl::Execute ({input		=> \'[- @ar = (a1, b2, c3) -] ',
						 mtime      => 1,  
						 inputfile	=> 'table',
						 req_rec    => $r}) ;
print "\n 3.) Include from memory with passing of variables
\n" ;
$MyPackage::Interface::Var = 'Some Var' ;
Embperl::Execute ({input		=> \'Transfer some vars [+ $Var +] !
',
						 inputfile	=> 'Var',
						 mtime      => 1,
						 'package'  => 'MyPackage::Interface',
						 req_rec    => $r}) ;
print "\n 4.) Change the variable, but not the code
\n" ;
$MyPackage::Interface::Var = 'Do it again' ;
# code is the same, so give the same mtime and inputfile to avoid recompile
# Note you get problems is you change the code, but did not restart the server or
# change the value in mtime. So make sure if you change something also change mtime!
Embperl::Execute ({input		=> \'Transfer some vars [+ $Var +] !
',
						 inputfile	=> 'Var2',
						 mtime      => 1,  
						 'package'  => 'MyPackage::Interface',
						 req_rec    => $r}) ;
print "\n 5.) Use \@param to pass parameters
\n" ;
Embperl::Execute ({input		=> \'Use @param to transfer some data ([+ "@param" +]) !
',
						 inputfile	=> 'Param',
                         debug      => 0xffffdffd,
    					 req_rec    => $r,
						 param      => [1, 2, 3, 4] }
						 ) ;
print "\n 6.) Use \@param to pass parameters and return it
\n" ;
my @p = ('vara', 'varb') ;
print "\n \$p[0] is $p[0] and \$p[1] is $p[1]" ;
Embperl::Execute ({input		=> \'
Got data in @param ([+ "@param" +]) !
[- $param[0] = "newA" ; $param[1] = "newB" ; -]Change data in @param to ([+ "@param" +]) !
',
						 inputfile	=> 'Param & Return',
						 req_rec    => $r,
						 param      => \@p }
						 ) ;
print "\n \$p[0] is now $p[0] and \$p[1] is now $p[1]" ;
print " 7.) Presetup \%fdat and \@ffld
\n" ;
my %myfdat = ('test' => 'value',
              'fdat' => 'text') ;
              
my @myffld = sort keys %myfdat ;             
Embperl::Execute ({input		=> \'
| [+ $ffld[$row] +] | [+ do { local $^W = 0 ; $fdat{$ffld[$row]} } +] | 
',
						 inputfile	=> 'fdat & ffld',
						 req_rec    => $r,
						 fdat  => \%myfdat,
						 ffld  => \@myffld}
						 ) ;
print "\n 8.) Inculde a file
\n" ;
Embperl::Execute ({inputfile	=> '../inc.htm', input_escmode => 7,
						 req_rec    => $r}) ;
print "\n 9.) Inculde a file and return output in a scalar
\n" ;
my $out ;
Embperl::Execute ({inputfile	=> '../inc.htm', input_escmode => 7,
						 output     => \$out,
						 req_rec    => $r}) ;
print "\n$out
\n" ;
print "\n 10.) Test \$req_rec inside Execute
\n" ;
Embperl::Execute ('../reqrec.htm') ;
print "\n 11.) Done :-)
\n" ;
print "\n";