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

Please anyone would know how I could make the sub weather get its results processed by the index sub and than have the substitutions done in the html template? I got it to work but the result on the browser is the code literally printed on the page and not the result for weather.pl. Thank you for the BIG help! Could be a better way to bring the weather.pl program into the html template in a different way? Thank you for the BIG help!!!!

sub index { #&top_nav; &weather; #$weatherData = weather(); #$weatherData = &weather(); #$weatherData = &weather; #$weatherData = main(); &date; #read in template page for INDEX.HTML################################# +###### my $templateFileIndex="../temp_test/index.html"; open(INFILE,"<$templateFileIndex"); my @templatePageIndex=<INFILE>; close(INFILE); #condense page array into one scalar variable my $tIndex=join("",@templatePageIndex); #search-and-replace on date, nav bar and variables $tIndex=~s/%%INSERT DATE HERE%%/$liveDate/g; #my $weatherData= &weather; $tIndex=~s/%%WEATHER2%%/$test/g;#This one works jus fine I can make th +e substitution. #It doesn't work #$tIndex=~s/%%WEATHER%%/$weatherData2/g; #$tIndex=~s/%%WEATHER%%/$weatherData/g; #$tIndex=~s/%%WEATHER%%/&weather/g; #$tIndex=~s/%%WEATHER%%/$weather/g; #done, output page to browser print $tIndex; exit } ###################################################################### +###### sub weather { # read in footer template $foot="c:/progra~1/apache~1/apache/cgi-bin/weather/weather.pl"; #This +program print a weather forecast into html page template open(MYFOOT,"<$foot"); @footerPage=<MYFOOT>; close(MYFOOT); #condense page array into one scalar variable $weather=join("",@footerPage); $test="Test variable, passing value to html template"; return $weather; }

Replies are listed 'Best First'.
Re: Template Parsing
by davorg (Chancellor) on Sep 18, 2002 at 13:04 UTC

    Why reinvent the wheel. There are plenty of templating systems available for Perl. Personally, I like the Template Toolkit.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I would also recommend seeing this node - specifically this link to perrin's excellent article, for more choices and the differences between them.
      Hi, I understand that there are many other template tools out there, but the solution for what I am doing require that I use what I am trying to do here. But thanks for your advice, anyway would you know how I could fix the problem using the code I have?
Re: Template Parsing
by perrin (Chancellor) on Sep 18, 2002 at 16:46 UTC
    This code has problems. The biggest one is that it is using global variables to pass things around between subs. This is a very messy and error-prone style. I would strongly encourage you to "use strict" and -w and to use explicitly passed variables with your subs. For example: my $weather_template = weather(); Your loading of files would be more efficient if you slurp them up like this:
    { local $/; open(MYFOOT,"<$foot"); my $weather_template = <MYFOOT>; close(MYFOOT); }
    If this simple replace is really enough for your template, then go with it. However, if you see any need for fancier constructs like loops and conditionals, please try using a module from CPAN for it.

    You don't really provide enough information to determine why you are seeing code in the browser (HTML code? Perl code?), but I don't see you printing a header before sending this content. You may also have your web server or permissions set up wrong, preventing it from executing your code.