perl-asp mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Theo E. Schlossnagle" <je...@omniti.com>
Subject Re: RFC: subroutines & script recompilation
Date Tue, 11 Feb 2003 23:18:27 GMT
Josh Chamas wrote:
> The problem is that when subroutines are defined in a script,
> they create closure issues as discussed here:
> 
>   http://www.apache-asp.org/style.html#Do%20not%20definc430b103
> 
> The greater problem is that resolving these closure issues can
> be a hair pulling event, and unless one gets into mod_perl
> and Apache::ASP significantly, this may be overlooked.  I would
> like Apache::ASP to be more newbie friendly.
> 
> The solution is whenever a subroutine definition is detected as in:
> 
> sub some_name {
> 
> }

We use sub routines in pages as closures with _great_ success.  I don't think
the above suggested modification will effect our use at all.  We don't used
named subroutines at all, only lambda functions (a.k.a. closures).

An example:

We have a Page.pm module, it allows you to add content to it and set titles
and breadcrumbs (specific to the concept of the website).  The "addcontent"
method takes either:
   (1) SCALAR (textual content)
   (2) SCALAR REF (reference to textual content)
   (3) SCALAR, ARRAY REF (filename, args -- for an internal include)
   (4) CODEREF, ARRAY REF (code to be executed during template rendering with 
args)

We have a set (usally small) of template files that take a Page object as an
argument.

So, assuming we have a pagetemplate 'template.inc'. My site subpages would
look like:

use strict;
use Page;

my $page = Page->new();
$page->title('Test Page');
my $confusing = 'great this is';
$page->addcontent(sub {
    my ($page, $Request, $Response) = @_;
%>

Cool ASP style content using _only_ what we get from @_...
<% if($Request->QueryString('user')) { %>
      Here is some info about that user.
<% } %>

However, our $confusing isn't in our scope, so we likely won't expect <%=
$confusing %>.

<% }, [ $Request, $Reponse ]);
$confusing = 'careful you must be.';
$Response->Include('template.inc', $page);


Using this method is similar to:

$page->addcontent($Response->TrapInclude('pagecontent.inc', $page));

With the advantage that you don't have to have a seperate file, and the
distinct disadvantage of "use strict;" not working they way you want.
The lamba function has a sub-lexical scope of the master Apache::ASP page that
it was compiled in... So the $confusing part above must be avoided by practice
-- use strict won't help you at all.  Assuming you can wrap your mind around
the $confusing problem demonstrated above (and never do dumb things like
that), then this approach works wonders.

Once you "swallow" the lamdba functions psychologically, it makes for very
maintainable code as well as very clean separatation of business
logic/rendering logic/presentation.  It allows all the core logic to be in
backing perl modules were it should be, but still separating further the
"template" from the rendering logic.

Perl Modules for business logic.
Apache::ASP Pages for _what_ and _how_ to put in a page
Apache::ASP 'templates' to render them.

A site for example that works this way is: www.shopbiltmore.com and it's 27
brothers (see http://66.77.52.12/).  To give an idea:

   o 3 template files in total (for all 28 sites)
	(>99% HTML)
   o the average page (exposed to user) is 50 lines of ASP code,
	similar in nature to the code sample above, to process
	forms and instantiate perl classes and manipulate
	the resulting objects.
	(<5% HTML)
   o thousands of lines of completely presentation separated
	perl modules representing the data model an business
	logic.
	(0% HTML)

The Apache::ASP system allows us to create web solutions with a truly pleasing
amount of code reuse, elegant separation of logic and presentation and minimal
amount of effort to maintain.

Yes Josh, I real (thought out) testimonial is on the way.

-- 
Theo Schlossnagle
Principal Consultant
OmniTI Computer Consulting, Inc. -- http://www.omniti.com/
Phone:  +1 410 872 4910 x201     Fax:  +1 410 872 4911
1024D/82844984/95FD 30F1 489E 4613 F22E  491A 7E88 364C 8284 4984
2047R/33131B65/71 F7 95 64 49 76 5D BA  3D 90 B9 9F BE 27 24 E7



---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


Mime
View raw message