http://www.perlmonks.org?node_id=464015

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is a bit of a stretch. But here it goes. I'll list my code first

index.cgi
#!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use vars qw($form); ##################################################### #Import our custom modules used in the main site # ##################################################### BEGIN {push (@INC,"modules/site"); push (@INC,"modules/custom"); } use databaseFunctions; use moduleSecurity; use forms; use template; ######################## #Connect to Database # ######################## our $db = new databaseFunctions; #declare a new database object $db->connect(); #Connect to the database ######################## #Do Form Stuff # ######################## our $form = new CGI; #We need to find out what URL the user is calling and see if its in ou +r #Portal List my $queryString = "SELECT portal from portalUrls where url like '\%$EN +V{'HTTP_HOST'}'"; my $dbSelect = $db->query($queryString); print "Content-type: text/plain\n\n"; #This will be removed, and other + header stuff will be added to each module itself if($dbSelect->{0}{portal} ==0) { print "No Document Found"; } else { #The Portal was found, now to see if its active $queryString = "SELECT active,defaultModule from portalSettings wh +ere id =" . $dbSelect->{0}{portal}; $dbSelect = $db->query($queryString); if($dbSelect->{0}{active} eq "n") { #This is where we would normally print a page saying the porta +l is temporarily unavailable #But for now, this will do print "At this time the site you have requested is unavailable +. Please check back later"; } else { #The Portal is Active. That means defaultModule is available #We need to check if this module is allowed to be used without + authentication my $secCheck = new moduleSecurity(); my $secReturn = $secCheck->checkModule($dbSelect->{0}{defaultM +odule}); my $templateFile = "site/main"; my $tpl = new template; my $returnedtpl = $tpl->parse($templateFile,"print"); if($secReturn eq "y") { print "Security Required"; } else { #my $module = $dbSelect->{0}{defaultModule}; require "$dbSelect->{0}{defaultModule}.pm"; my $returned = $dbSelect->{0}{defaultModule}->mainModule; print $returned; } } }
In the Above code the section
my $templateFile = "site/main"; my $tpl = new template; my $returnedtpl = $tpl->parse($templateFile,"print");
Calls my custom template module as loaded above. This part works fine The value $templateFile = "site/main" is a directory path The contents of "site/main.tpl" is
Some **** <b><%include variable="var1"%></b> <br> <%include module="mod2"%> Some More **** <br> <%include template="site/inctemp"%> <br> Some Random **** <br> <%include menu="menu1"%> <br>
The line
<%include template="site/inctemp"%>
Tells the template module it needs to load yet another template into this location. The template is "site/inctemp.tpl" Then inside "site/inctemp.tpl" it calls "site/inc2.tpl" (these are just development files and have no real world baring) Sounds simple. The template module itself contains this code
package template; use DBI; use strict; use warnings; use variables; BEGIN{ push(@INC,"modules/site"); push(@INC,"modules/custom"); } sub new { my ($class) = @_; my $self = {_template => undef, _method => undef, _filehandle= +>undef}; bless $self,$class; return $self; } sub parse { #use String::Random; #my $ran = new String::Random; my $returnTPL= ""; my ($self,$template,$method) = @_; $self->{_template} = $template if defined($template); $self->{_method} = $method if defined($method); #my $filehandle = $ran->randregex('\w\w\w\'); $self->{_filehandle} = "a121"; if(!(-e "templates/$self->{_template}.tpl")) { #template does not exists; print "Error... No such template exists"; } else { #template does exist open(tpl,"templates/$self->{_template}.tpl"); my $x=0; while(<tpl>) { if($_ =~ /\<\%include/) { #We found a template request, now we need to find +what it is if($_ =~ /\<\%include\s?(\w.*)\=[\"\']([a-zA-Z0-9\ +/].*)[\"\']\%\>/) { my $type = $1; my $val = $2; if($type =~ /variable/i) { my $vars = variables->get(); $_ =~ s/<\%include\s?(\w.*)\=[\"\']([a-zA- +Z0-9].*)[\"\']\%\>/$vars->{$val}/; } if($type =~ /template/i) { #create a new template instance so this te +mplate #doesn't get deleted my $innertpl = new template; my $_innertpl = $innertpl->parse($val,"ret +urn"); print $_innertpl; #$_ =~ s/<\%include\s?(\w.*)\=[\"\']([a-zA +-Z0-9\/].*)[\"\']\%\>/$_innertpl/; } if($type =~ /menu/i) { #query the database for the menu in questi +on #Butcher, and fix my $menu = "The Menu Items"; $_ =~ s/<\%include\s?(\w.*)\=[\"\']([a-zA- +Z0-9].*)[\"\']\%\>/$menu/; } if($type =~ /module/i) { #Activate the modules include function require "$val.pm"; my $returnedData = $val->includeModule(); $_ =~ s/<\%include\s?(\w.*)\=[\"\']([a-zA- +Z0-9].*)[\"\']\%\>/$returnedData/; } } } if($self->{_method} eq "print") { print $_; } if($self->{_method} eq "return") { $returnTPL = join("",$returnTPL,$_); #print $returnTPL; } } close(tpl); } if($self->{_method} eq "return") { return $returnTPL; } } 1;
The Problem...
For whatever reason After it goes into the templates within the templates it stops at the
<%include template>
command and never continues and its beyond me as to why. The real world script is running at http://fidelitysoft.net/cms/ The directories are (http://fidelitysoft.net/cms) All files in those directories are readable from the web so you can see the code thats being done The Module that is malfunctioning is template.pm The index script is available at http://fidelitysoft.net/cms/index.txt Can someone tell me why my scripts are not returning after it reads into another template? Your assistance is greatly, hugely appriciated.

Janitored by holli - added readmore tags