Apache-ASP

 view release on metacpan or  search on metacpan

site/style.html  view on Meta::CPAN

		<tr>
		
			<td valign=top >
			<font face="lucida console" size=-1>
			<a href=#UseStrict>UseStrict</a>
			</font>
			</td>
		
			<td valign=top >
			<font face="lucida console" size=-1>
			<a href=#Use%20global.ae21f52dc>Use global.asa's Script_On* Events</a>
			</font>
			</td>
							
		</tr>
		
		<tr>
		
			<td valign=top >
			<font face="lucida console" size=-1>
			<a href=#Do%20not%20definc4a0555f>Do not define subroutines in scripts.</a>
			</font>
			</td>
		<td>&nbsp;</td>					
		</tr>
			
	</table>
	<hr size=1>
	<p>

	<p>
	<a name=UseStrict></a>
	<font face=verdana><font class=title size=+0 color=#555555><b>UseStrict</b></font>
<font face="courier new" size=3><pre>
</pre></font>One of perl&#39;s blessings is also its bane, variables do not need to be
declared, and are by default globally scoped.  The problem with this in 
<a href=http://perl.apache.org><font size=-1 face=verdana><b>mod_perl</b></font></a> is that global variables persist from one request to another
even if a different web browser is viewing a page.  
<font face="courier new" size=3><pre>
</pre></font>To avoid this problem, perl programmers have often been advised to
add to the top of their perl scripts:
<font face="courier new" size=3><pre>
  use strict;
</pre></font>In Apache::ASP, you can do this better by setting:
<font face="courier new" size=3><pre>
  PerlSetVar UseStrict 1
</pre></font>which will cover both script &amp; global.asa compilation and will catch 
&quot;use strict&quot; errors correctly.  For perl modules, please continue to
add &quot;use strict&quot; to the top of them.
<font face="courier new" size=3><pre>
</pre></font>Because its so essential in catching hard to find errors, this 
configuration will likely become the default in some future release.
For now, keep setting it.</font>
	
	<p>
	<a name=Do%20not%20definc4a0555f></a>
	<font face=verdana><font class=title size=+0 color=#555555><b>Do not define subroutines in scripts.</b></font>
<font face="courier new" size=3><pre>
</pre></font>DO NOT add subroutine declarations in scripts.  Apache::ASP is optimized
by compiling a script into a subroutine for faster future invocation.
Adding a subroutine definition to a script then looks like this to 
the compiler:
<font face="courier new" size=3><pre>
  sub page_script_sub {
    ...
    ... some HTML ...
    ...
    sub your_sub {
      ...
    }
    ...
  }
</pre></font>The biggest problem with subroutines defined in subroutines is the 
side effect of creating closures, which will not behave as usually
desired in a <a href=http://perl.apache.org><font size=-1 face=verdana><b>mod_perl</b></font></a> environment.  To understand more about closures,
please read up on them &amp; &quot;Nested Subroutines&quot; at:
<font face="courier new" size=3><pre>
  <a href=http://perl.apache.org/docs/general/perl_reference/perl_reference.html>http://perl.apache.org/docs/general/perl_reference/perl_reference.html</a>
</pre></font>Instead of defining subroutines in scripts, you may add them to your sites
global.asa, or you may create a perl package or module to share
with your scripts.  For more on perl objects &amp; modules, please see:
<font face="courier new" size=3><pre>
  <a href=http://perldoc.perl.org/perlobj.html>http://perldoc.perl.org/perlobj.html</a>
</pre></font>
	
	<p>
	<a name=Use%20global.ae21f52dc></a>
	<font face=verdana><font class=title size=+0 color=#555555><b>Use global.asa's Script_On* Events</b></font>
<font face="courier new" size=3><pre>
</pre></font>Chances are that you will find yourself doing the same thing repeatedly
in each of your web application&#39;s scripts.  You can use Script_OnStart
and Script_OnEnd to automate these routine tasks.  These events are
called before and after each script request.
<font face="courier new" size=3><pre>
</pre></font>For example, let&#39;s say you have a header &amp; footer you would like to 
include in the output of every page, then you might:
<font face="courier new" size=3><pre>
 # global.asa
 sub Script_OnStart {
   $Response-&gt;Include(&#39;header.inc&#39;);
 }
 sub Script_OnEnd {
   $Response-&gt;Include(&#39;footer.inc&#39;);
 }
</pre></font>Or let&#39;s say you want to initialize a global database connection
for use in your scripts:
<font face="courier new" size=3><pre>
 # global.asa
 use Apache::DBI;   # automatic persistent database connections
 use DBI;

 use vars qw($dbh); # declare global $dbh

 sub Script_OnStart {
   # initialize $dbh
   $dbh = DBI-&gt;connect(...);

   # force you to explicitly commit when you want to save data
   $Server-&gt;RegisterCleanup(sub { $dbh-&gt;rollback; });
 }



( run in 1.999 second using v1.01-cache-2.11-cpan-39bf76dae61 )