perl-asp mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joshua Chamas <jos...@chamas.com>
Subject Re: Global Variables in ASP
Date Wed, 13 Feb 2002 00:33:48 GMT
Mike Martinet wrote:
> 
> Greetings,
> 
> Can anyone tell me what is the best method for sharing variables/data
> structures across Active Server Pages?
> 

If its data global to all pages, I would just defined the structure
in global.asa, like:

  PerlSetVar UseStrict 1 # in httpd.conf, makes you declare globals

# global.asa
use vars qw(%data); # declare global %data
%data = (...);

> I have two examples:
> 
> 1:Declared structures
> 
> How do I use a hash defined in one page as a template in another?, i.e.:
> 
> [Common.asp]
> %fruit = qw( banana, apple, orange )
> 
> [AddItem.asp]
> <!-- include file ./Common.asp -->
> 
> yada, yada
> 
> %newFruit = %fruit;  ???

To do this, I might use return values from includes, treating them
as subroutines, which they are.  You might also consider abstracting
this data into some perl module, and making and object of it,
like My::Fruit

## include style
[common.asp]
<% 
  %fruit = ();
  return \%fruit;
%>
[AddItem.asp]
<% my $fruit = $Response->Include('common.asp'); %>

using the OO approach though, you might:
  <% my $fruit = My::Fruit->new(); %>

> 
> ########################################33
> 
> 2:Initialized variables
> 
> [Common.asp]
> sub initDate {
> 
>     my $tNow, $tHour;
> 
>     $tNow = localtime();
>     $tHour = substr( $tNow, 11, 2 );
> 
> }
> 
> [Update.asp]
> <!-- include file ./Common.asp -->
> 
> use vars $tNow ???
> 
> &initDate;
> 

ASP includes should not be used to get around having perl modules
and the like to encapsulate your logic.  global.asa can be used
as a default perl package for your ASP scripts, but if you
want to reuse these methods outside of ASP, like in some cron job,
you should put them in a separate perl module...

# global.asa
sub initDate {
  ...
  ($tNow, $tHour);
}

# Update.asp
<% my($now, $hour) = &initDate(); %>

If you do not want to define initDate() in global.asa, you could
define a package of helper functions like:

# in Common.pm somewhere in @INC, @INC can be modified with use lib ()
package Common;
@ISA = qw(Exporter);
use strict;
@EXPORT_OK = qw(initDate);

sub initDate {...};

1;

Then in global.asa:
  use Common;

This will import all the exported symbols from Common into your
ASP application namespace for all your pages to use.  One caveat
is that you can only have one Common package per web server. 
People that want to have separate perl module per VirtualHosts
run into problems, since the perl %INC namespace is shared
across all sites running under mod_perl.

-- Josh
_________________________________________________________________
Joshua Chamas                           Chamas Enterprises Inc.
NodeWorks Founder                       Huntington Beach, CA  USA 
http://www.nodeworks.com                1-714-625-4051

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


Mime
View raw message