You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Dave Morgan <dv...@telusplanet.net> on 2002/01/08 06:39:46 UTC

mod-perl, modules and initializations

Hi All,
	My environment is Apache, mod-perl, Apache::DBI, oracle backend,  
	everything works fine. The problem is always MY code :)
	
	I'm trying to figure out the correct/efficient way to initialize
constants in a 		module. What I need to do is initialize various
constants when Apache is started, 		whether hard coded, read in from a
file or extracted from a database. I have managed 		to thoroughly
confuse myself and have the following questions.

	What is the difference between how a BEGIN block and an anonymous block
	in a module loaded into mod_perl? At different times I understand,
though
	I probably don't understand the implications of that in a mod-perl
environment. 
	The reason I ask is: as a begin block my initializations do not take
effect 
	yet as an anonymous block they do. Or is this based in my lack of 
	understanding of perl? 

	Are anonymous blocks in a module only read and executed when mod-perl
first loads 		them, ie once?

	Another problem is when I try to build a SELECT HTML element with a
call to 
	the CGI module. In my anonymous block all of a sudden the HTML form
variables 
	are no longer available with the CGI::param call.  Yet I can build the
select 			element later in the cgi scripts using the same variables
without a problem. 
	Huh? Did I mention I'm confused and my head hurts  :}

	In a simpler line, should I have a use DBI() in startup.pl as well as
the 
	PerlModule Apache::DBI in httpd.conf?

	I will summarize and post responses.
TIA 
Dave

Code snippets

####################################################
# from httpd.conf
AddModule mod_perl.c

PerlSetEnv ORACLE_HOME /opt/oracle
PerlSetEnv DSOURCE dbi:Oracle:IMG
PerlSetEnv DATACLERK  public_tst
PerlSetEnv PSSWD  opentoall

PerlModule Apache::Registry
PerlModule Apache::DBI
PerlWarn On
PerlTaintCheck On

PerlRequire /opt/apache/conf/startup.pl


#################################################################
#all of startup.pl
	 
use CGI ();
use COMMON ();
use WSERVICES ();

Apache::DBI->connect_on_init($ENV{"DSOURCE"}, $ENV{"DATACLERK"},
$ENV{"PSSWD"},
                { PrintError => 1, #warn() on errors
                  RaiseError => 0, # don't die on error
                  AutoCommit => 1, # commit executes immediately
                }
            );


###################################################################
# WSERVICES.pm
package WSERVICES;
require Exporter;

use strict;

use COMMON; # wrapper for basic file and db manipulation
use DBD::Oracle qw(:ora_types);
use CGI qw/:html :cgi :form/;

our(@DB_NAMES $FILE_NAME $NUMPERPAGE
	$GEOCODED_LIST $LOCATION);

our @ISA	= qw(Exporter);
our @EXPORT	= qw(@DB_NAMES $FILE_NAME $NUMPERPAGE,
		$GEOCODED_LIST $LOCATION example_sub);

# As a begin block none of the following initializations occur 
# all subs in COMMON.pm 
{
	$NUMPERPAGE = 12;
	$FILE_NAME = &Readfile('file.txt');  #see COMMON.pm

	&Create_DB_Connection;  
	@DB_NAMES = @{&Get_Select_List()};
	&Destroy_DB_Connection;  

#following code destroys variables passed through CGI param sub
# yet exact same call will work if called in cgi script or subroutine
	
   	$GEOCODED_LIST = scrolling_list(-name=>'CITY_LOC',
				-default=>$LOCATION,
				-values=>\@DB_NAMES,
				-size=> 1);


}
	

sub examplesub{return 43;}

1;
######################################################


-- 
Dave Morgan
dvmrgn@telusplanet.net
403 399 2442
"perl Makefile.PL; make install;" is just way too convenient

Re: Ip Address On Outbound

Posted by "Doran L. Barton" <fo...@iodynamics.com>.
Not long ago, John Buwa proclaimed...
> I havea question on outbound ips. I am writing a program that a user can
> enter an address and a spider will go and retrieve only the information that
> was request and then display the results to the enquiring user. My question
> is though, i have grabed the users ip address as the initiator of the
> request and i would like to use that ip address (The users) to send the http
> request instead of my systems addres. How would i changed outgoing requests
> to reflect the invoking users ip and not my systems ip?

Being as how the IP address is part of the TCP/IP protocols and not HTTP,
I can't see how Apache (or mod_perl) can do anything about that. You can't
spoof IPs at the application layer if you're doing network transactions.

-=Fozz

-- 
-------------------------------------------------------------------------
Doran L. Barton <fo...@iodynamics.com> - Chief Super Hero - Iodynamics LLC
< http://www.iodynamics.com/ > - Linux solutions and dynamic websites
 "Pepsi brings your ancestors back from the grave."
    -- 'Pepsi Comes Alive' as originally translated into Chinese

Re: Ip Address On Outbound

Posted by Luciano Miguel Ferreira Rocha <st...@nsk.yi.org>.
On Wed, Jan 09, 2002 at 12:27:12AM -0500, John Buwa wrote:
> How would i changed outgoing requests
> to reflect the invoking users ip and not my systems ip?

You can't just change the ip you use to connect to other systems...

But you may have some luck in using the Via: and X-Forwarded-For: headers:
GET / HTTP/1.0
Via: 1.0 agent.mydmn.net:3128 (Yet Another Agent/0.0.1.0a)
X-Forwarded-For: 192.168.0.1

Regards,
Luciano Rocha

-- 
Luciano Rocha, strange@nsk.yi.org

The trouble with computers is that they do what you tell them, not what
you want.
                -- D. Cohen

Ip Address On Outbound

Posted by John Buwa <jo...@tcpbbs.net>.
 Hello,

I havea question on outbound ips. I am writing a program that a user can
enter an address and a spider will go and retrieve only the information that
was request and then display the results to the enquiring user. My question
is though, i have grabed the users ip address as the initiator of the
request and i would like to use that ip address (The users) to send the http
request instead of my systems addres. How would i changed outgoing requests
to reflect the invoking users ip and not my systems ip?

Thanks in advance,
John


Sample Code for CGI initialization problem

Posted by Dave Morgan <dv...@telusplanet.net>.
Hi All,
	I have attached sample code that illustrates the problem

	Load COMMON.pm on startup, put html and cgi in proper places,
	configure to run the cgi as an Apache::Registry script.

	In a browser, load index html, fill out the fields and submit.
	Your input will not be echoed back

	Stop apache, remove the BEGIN block from COMMON.pm, restart, test.

	Input is now echoed back.

HTH
Dave

#######################################
package COMMON;

require Exporter;

use strict;

use CGI qw/:standard/;

our (@ISA, @EXPORT);

our ($USERID, $PASSWORD, $select_list);

@ISA	= qw(Exporter);
@EXPORT	= qw($USERID $PASSWORD $select_list);
	  
BEGIN{
	$select_list = scrolling_list(-name=>'TEST',
                                -values=>[1,2,3,4],
                                -default=>'1',
                                -size=> 1);
}

1;
# END
############################################
#login.cgi
#!/opt/bin/perl -T

use strict;

use CGI qw/:standard :cgi-lib/;
use COMMON;

# Get Form Values
$USERID			= param("USERID");
$PASSWORD               = param("PASSWORD"); 

print header;
print "$USERID, $PASSWORD";

#END
###############################################
#index.html
<html>
<BODY BGCOLOR="white">

<form action="/cgi-bin/login.cgi" method="POST">
<table BORDER="0" cellspacing="0" cellpadding="0" width="250">
<tr><td colspan="2" ><font size="2" FACE="helvetica,arial">
Login Name:
</td><td>
<input type="text" name="USERID" size="20" maxlength="20">
</td></tr>
<tr><td colspan="2" ><font size="2" FACE="helvetica,arial">
Password
</td><td>
<input type="password" name="PASSWORD" size="20" maxlength="20">
</td></tr>
<tr><td colspan="4" >
----------------------------------
</td></tr> 
<tr><td>
<input type="submit" value="Log in">
</td> <td colspan="2" ><font size="2" FACE="helvetica,arial">
Test
</td></tr></table>
</form>
</body>
</html>
#END


-- 
Dave Morgan
dvmrgn@telusplanet.net
403 399 2442

Re: mod-perl, modules and initializations

Posted by ___cliff rayman___ <cl...@genwax.com>.
the guide is your friend:
http://perl.apache.org/guide

Dave Morgan wrote:

>         What is the difference between how a BEGIN block and an anonymous block

http://thingy.kcilink.com/modperlguide/porting/BEGIN_blocks.html

>
>         Another problem is when I try to build a SELECT HTML element with a
> call to
>         the CGI module. In my anonymous block all of a sudden the HTML form
> variables
>         are no longer available with the CGI::param call.  Yet I can build the
> select                  element later in the cgi scripts using the same variables
> without a problem.

the above is not clear.

>
>         In a simpler line, should I have a use DBI() in startup.pl as well as
> the
>         PerlModule Apache::DBI in httpd.conf?

you really only need one or the other.  not sure if one is preferable with
Apache::DBI.

http://thingy.kcilink.com/modperlguide/config/PerlModule_and_PerlRequire_Direc.html

>
>
>         I will summarize and post responses.

not necessary.  as you can see, the guide is a professional job
of summarizing these types of questions.  i think you'll find it invaluable.
there is also a book by the author of mod_perl:
http://www.modperl.org/

if you want to do any serious mod_perl programming, you probably should read
through both.


--
___cliff rayman___cliff@genwax.com___http://www.genwax.com/



Re: mod-perl, modules and initializations

Posted by Perrin Harkins <pe...@elem.com>.
On Tuesday 08 January 2002 08:16 pm, Dave Morgan wrote:
> I'm trying to populate select boxes(or other input types)for my HTML
> pages.
> An example would be a drop down list of states and/or provinces. A large
> number
> of these are populated from lookup tables in the dba and are relatively
> static.

Okay, I suspect the problem is that whenever you get a new request the setup 
you did for CGI.pm gets cleared.  You should store the static data in a 
global, and then populate the CGI widget with it on every request.

- Perrin

Re: mod-perl, modules and initializations

Posted by Dave Morgan <dv...@telusplanet.net>.
I'm trying to populate select boxes(or other input types)for my HTML
pages. 
An example would be a drop down list of states and/or provinces. A large
number
of these are populated from lookup tables in the dba and are relatively
static. 


I understand there is no form data at that time, however, when
the server is up and running (and I am accessing it as a client)
is when I am having the problem of extracting the data.

Hope this is clearer

Dave

Perrin Harkins wrote:
> 
> > By load stage I mean BEGIN blocks, anonymous
> > subroutines in packages loaded at startup, or even named
> > subroutines called from startup.pl
> 
> All of those things happen during server startup, before any request has
> been submitted.  There is no form data at that time.
> 
> Maybe if you could explain what you're trying to accomplish by calling
> CGI methods during initialization, someone could suggest an alternative
> way to do it.
> 
> - Perrin

-- 
Dave Morgan
dvmrgn@telusplanet.net
403 399 2442

Re: mod-perl, modules and initializations

Posted by Perrin Harkins <pe...@elem.com>.
> By load stage I mean BEGIN blocks, anonymous
> subroutines in packages loaded at startup, or even named
> subroutines called from startup.pl

All of those things happen during server startup, before any request has
been submitted.  There is no form data at that time.

Maybe if you could explain what you're trying to accomplish by calling
CGI methods during initialization, someone could suggest an alternative
way to do it.

- Perrin


Re: mod-perl, modules and initializations

Posted by Dave Morgan <dv...@telusplanet.net>.
Hi All,

Thanks to Perrin's help I have been able to isolate 
all my current problems down to whenever I make a call to

CGI::scrolling_list(...);

in a piece of code that is executed in the load stage of
mod_perl I am unable to extract any values from my forms
using

CGI::param("field_name ");

in my cgi scripts. By load stage I mean BEGIN blocks, anonymous 
subroutines in packages loaded at startup, or even named 
subroutines called from startup.pl

If I remove the call from the load stage I can issue the scrolling_list 
call from any cgi (or packaged sub called from a script) and everything 
works fine. 

Versions: perl 5.6.1, mod-perl 1.26, CGI 2.78

Any ideas? other than go bug the CGI.pm people, I'm already on my way :)
 

Perrin Harkins wrote:

snip ..
> It looks to me like you are confused about "our" and BEGIN.  If you change
> the "our" to a "use vars" I think it will fix your problems.  This is not
> mod_perl-specific.
snip...

I was able to get BEGIN blocks to work with "our", after I changed
everything to the
"use vars" syntax, and back. No idea what changed, don't really care,
just happy :)

Both the mod_perl docs and perl 5.6.1 docs are pushing the use of "our"  

Dave
-- 
Dave Morgan
dvmrgn@telusplanet.net
403 399 2442
I cut perl the way I cut C, badly.

Re: mod-perl, modules and initializations

Posted by Perrin Harkins <pe...@elem.com>.
> What is the difference between how a BEGIN block and an anonymous block
> in a module loaded into mod_perl?

It looks to me like you are confused about "our" and BEGIN.  If you change
the "our" to a "use vars" I think it will fix your problems.  This is not
mod_perl-specific.

> Are anonymous blocks in a module only read and executed when mod-perl
> first loads them, ie once?

The block in your example is not inside of a subroutine, so it will only be
called once when the module is loaded.

> Another problem is when I try to build a SELECT HTML element with a
> call to
> the CGI module. In my anonymous block all of a sudden the HTML form
> variables
> are no longer available with the CGI::param call.  Yet I can build the
> select element later in the cgi scripts using the same variables
> without a problem.

I'm guessing it's more scoping problems with "our."

> In a simpler line, should I have a use DBI() in startup.pl as well as
> the
> PerlModule Apache::DBI in httpd.conf?

You need to use both Apache::DBI and DBI somewhere.  Either place is fine.
I usually pull in lots of modules, so it's easier to do in startup.pl.

- Perrin