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

im trying to make a mod_perl module, but find that my script is very limited in its lay out, and cant figure out how to use the sub routines correctly. But my main problem at the moment is:-

use Apache2::RequestIO (); # for $r->puts(<<"END");

i would like to use cgi.pm forms and stuff after END which is filled with half a html template then use $r->puts(<<"END"); for the other half of the html template.

But the stuff in the middle (cgi.pm) doesnt display even if i use plain html instead of cgi.pm.

i have also tried print <<END; but this doesnt display anything, please could some1 help me understand where im going wrong.

maybe because its in the middle of a table?>


here is the code:-

package Apache2::CPH; use strict; use Apache2::RequestRec (); # for $r->content_type use Apache2::RequestIO (); # for $r->puts use Apache2::Const -compile => ':common'; use CGI; sub handler { my $q = new CGI; my $p = shift; my $time = scalar localtime(); $p->content_type('text/html'); my $uri = $p->uri; my $title = substr($uri, 1); if ($title =~ "home") { $title = "Home" } elsif ($title =~ "residential_mortgages") { $title = "Residential Mortgages" } elsif ($title =~ "commercial_mortgages") { $title = "Commercial Mortgages" } elsif ($title =~ "pensions") { $title = "Pensions" } elsif ($title =~ "investments") { $title = "Investments" } elsif ($title =~ "life_assurance") { $title = "Life Assurance" } elsif ($title =~ "general_insurance") { $title = "General Insurance" } elsif ($title =~ "contact_us") { $title = "Contact Us" } else { $title = "Home" }; my $content; if ($title =~ "Home") { $content = "Homepage" } elsif ($title =~ "Residential Mortgages") { $content = "Residential Mortgages" } elsif ($title =~ "Commercial Mortgages") { $content = "Commercial Mortgages" } elsif ($title =~ "Pensions") { $content = "Pensions" } elsif ($title =~ "Investments") { $content = "Investments" } elsif ($title =~ "Life Assurance") { $content = "Life Assurance" } elsif ($title =~ "General Insurance") { $content = "General Insurance" } elsif ($title =~ "Contact Us") { $content = "Contact Us" }; $p->puts(<<"END"); <HTML> <head> <title>$title</title> <script language="JavaScript"> <!-- function MM_displayStatusMsg(msgStr) { //v3.0 status=msgStr; document.MM_returnValue = true; } function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frame +s.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) +x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers +[i].document); if(!x && d.getElementById) x=d.getElementById(n); return x; } function MM_swapImage() { //v3.0 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for( +i=0;i<(a.length-2);i+=3) if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) +x.oSrc=x.src; x.src=a[i+2];} } function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i+ ++) x.src=x.oSrc; } function MM_preloadImages() { //v3.0 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array(); var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.le +ngth; i++) if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i +];}} } //--> </script> <style type="text/css"> <!-- .shadow { background-image: url(/images/home_r4_c10.gif); background-position: top; } .style4 { font-size: 12px; color: #FF0000; } --> </style> </head> <BODY> <table width="817" border="0" align="center" cellpadding="0" cellspaci +ng="0"> <tr> <td width="247"><img src="/images/spacer.gif" width="247" height="1 +" border="0" alt=""></td> <td width="69"><img src="/images/spacer.gif" width="69" height="1" +border="0" alt=""></td> <td width="70"><img src="/images/spacer.gif" width="70" height="1" +border="0" alt=""></td> <td width="69"><img src="/images/spacer.gif" width="69" height="1" +border="0" alt=""></td> <td width="71"><img src="/images/spacer.gif" width="71" height="1" +border="0" alt=""></td> <td width="68"><img src="/images/spacer.gif" width="68" height="1" +border="0" alt=""></td> <td width="71"><img src="/images/spacer.gif" width="71" height="1" +border="0" alt=""></td> <td width="69"><img src="/images/spacer.gif" width="69" height="1" +border="0" alt=""></td> <td width="64"><img src="/images/spacer.gif" width="64" height="1" +border="0" alt=""></td> <td width="15"><img src="/images/spacer.gif" width="15" height="1" +border="0" alt=""></td> <td width="4"><img src="/images/spacer.gif" width="1" height="1" bo +rder="0" alt=""></td> </tr> <tr> <td colspan="10"><img name="home_r1_c1" src="/images/home_r1_c1.gif +" width="813" height="161" border="0" alt=""></td> <td><img src="/images/spacer.gif" width="1" height="161" border="0" + alt=""></td> </tr> <tr> <td><img name="home_r2_c1" src="/images/home_r2_c1.gif" width="247" + height="44" border="0" alt=""></td> <td><a href="home" onMouseOut="MM_swapImgRestore();" onMouseOver="M +M_displayStatusMsg('Homepage');MM_swapImage('home_r2_c2','','/images/ +home_r2_c2_f2.gif',1);return document.MM_returnValue"><img name="home +_r2_c2" src="/images/home_r2_c2.gif" width="69" height="44" border="0 +" alt=""></a></td> <td><a href="residential_mortgages" onMouseOut="MM_swapImgRestore() +;" onMouseOver="MM_displayStatusMsg('Residential Mortgages');MM_swapI +mage('home_r2_c3','','/images/home_r2_c3_f2.gif',1);return document.M +M_returnValue"><img name="home_r2_c3" src="/images/home_r2_c3.gif" wi +dth="70" height="44" border="0" alt=""></a></td> <td><a href="commercial_mortgages" onMouseOut="MM_swapImgRestore(); +" onMouseOver="MM_displayStatusMsg('Commercial Mortgages');MM_swapIma +ge('home_r2_c4','','/images/home_r2_c4_f2.gif',1);return document.MM_ +returnValue"><img name="home_r2_c4" src="/images/home_r2_c4.gif" widt +h="69" height="44" border="0" alt=""></a></td> <td><a href="pensions" onMouseOut="MM_swapImgRestore();" onMouseOve +r="MM_displayStatusMsg('Pensions');MM_swapImage('home_r2_c5','','/ima +ges/home_r2_c5_f2.gif',1);return document.MM_returnValue"><img name=" +home_r2_c5" src="/images/home_r2_c5.gif" width="71" height="44" borde +r="0" alt=""></a></td> <td><a href="investments" onMouseOut="MM_swapImgRestore();" onMouse +Over="MM_displayStatusMsg('Investments');MM_swapImage('home_r2_c6','' +,'/images/home_r2_c6_f2.gif',1);return document.MM_returnValue"><img +name="home_r2_c6" src="/images/home_r2_c6.gif" width="68" height="44" + border="0" alt=""></a></td> <td><a href="life_assurance" onMouseOut="MM_swapImgRestore();" onMo +useOver="MM_displayStatusMsg('Life Assurance');MM_swapImage('home_r2_ +c7','','/images/home_r2_c7_f2.gif',1);return document.MM_returnValue" +><img name="home_r2_c7" src="/images/home_r2_c7.gif" width="71" heigh +t="44" border="0" alt=""></a></td> <td><a href="general_insurance" onMouseOut="MM_swapImgRestore();" o +nMouseOver="MM_displayStatusMsg('General Insurance');MM_swapImage('ho +me_r2_c8','','/images/home_r2_c8_f2.gif',1);return document.MM_return +Value"><img name="home_r2_c8" src="/images/home_r2_c8.gif" width="69" + height="44" border="0" alt=""></a></td> <td colspan="2"><a href="contact_us" onMouseOut="MM_swapImgRestore( +);" onMouseOver="MM_displayStatusMsg('Contact us');MM_swapImage('home +_r2_c9','','/images/home_r2_c9_f2.gif',1);return document.MM_returnVa +lue"><img name="home_r2_c9" src="/images/home_r2_c9.gif" width="79" h +eight="44" border="0" alt=""></a></td> <td><img src="/images/spacer.gif" width="1" height="44" border="0" +alt=""></td> </tr> <tr> <td colspan="10"><img name="/home_r3_c1" src="/images/home_r3_c1.gi +f" width="813" height="9" border="0" alt=""></td> <td><img src="/images/spacer.gif" width="1" height="9" border="0" a +lt=""></td> </tr> <tr> <td align="center" valign="top" colspan="9"> <table><tr> <td width="20%" align="center"><font color="#000000" size="2"><B +>$time</B></font></td> <td width="80%" align="center"><h1>$content</h1></td> </tr></table> END print "<font >© 2006 All Rights Reserved.</font>"; $p->puts(<<"END"); </td> <td rowspan="2" class="shadow">&nbsp;</td> <td><img src="/images/spacer.gif" width="1" height="185" border="0" + alt=""></td> </tr> <tr> <td colspan="9" bgcolor="#3300FF" align="center"> <font color="#FFFFFF"> © 2006 C P H All Rights Reserved. </font> </td> <td><img src="/images/spacer.gif" width="1" height="40" border="0" +alt=""></td> </tr> <tr> <td colspan="10"><img name="home_r6_c1" src="/images/home_r6_c1.gif +" width="813" height="8" border="0" alt=""></td> <td><img src="/images/spacer.gif" width="1" height="8" border="0" a +lt=""></td> </tr> <tr> <td align="center" colspan="9"> <font color="#000000" size="2"> Website Designed by <a href="mailto:s1mon">s1mon.</a> </font> </td> </tr> </table> </BODY></HTML> END return Apache2::Const::OK; }; 1;

Edit: g0n - readmore tags

Replies are listed 'Best First'.
Re: mod_perl2 module
by aquarium (Curate) on Apr 02, 2006 at 12:38 UTC
    if this is on a windows machine, put the following as the first executable line of code, which flushes buffers properly
    $| = 1;
    the hardest line to type correctly is: stty erase ^H

      I downvoted this answer. Why do you think the OP had a buffering problem?

      i have tried $| = 1; and print "<font >© 2006 All Rights Reserved.</font>"; still doesnt display.

      it is a windows machine.
        found a cure for my problem

        END $p->print($q->h1('hey')); $p->print (<<'END');


        the print function within Apache2::RequestIO (); allows me to use CGI.pm functions.