You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Michael Drons <md...@yahoo.com> on 2002/08/13 07:14:59 UTC

variables not changing with modperl??

I am using Apache::Registry (Apache 1.3.26) I am see
weird things happen with my scripts.  I have have "use
strict" in all of the scripts and I use my() for all
of my variables.  But I still have variables that
contain data from previous loads.  I see it in hashes
and arrays.  Especially, if I have an array that
contains 6 strings in load 1 and only 2 strings in
load 2.  In the second load of the script the array
will contain the 2 new strings and the 4 old strings. 
Everything I can find in docs says read the FAQ at 
http://perl.apache.org/faq/, which does not exists.
This link comes from the subscribe message.  I have
thought about using PerlRun, but a module I use
(AuthCookie) requies mod_perl.  How do I undefine or
reinit the variable?  I am currently using undef, but
it does not work.

Mike Drons

__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

Re: variables not changing with modperl??

Posted by Perrin Harkins <pe...@elem.com>.
Ken Y. Clark wrote:
> As for this and Perrin's comment about parsing on your own, the point
> is that you've written a lot of code that has already been written and
> debugged by a lot of really smart people.  There's no reason for you
> to be reading STDIN and spliting and all that.  If you're using
> mod_perl, then you really should do yourself a favor and read up on
> Apache::Request and Apache::Cookie for doing all this stuff.

Even if you are just using CGI, you shouldn't write your own parsing 
code like this.  Rather than try to explain why, I'll point you to this 
article on the subject:
http://perlmonks.org/index.pl?node_id=51012

- Perrin




Re: variables not changing with modperl??

Posted by "Ken Y. Clark" <kc...@logsoft.com>.
On Tue, 13 Aug 2002, Michael Drons wrote:

> Date: Tue, 13 Aug 2002 07:46:16 -0700 (PDT)
> From: Michael Drons <md...@yahoo.com>
> Reply-To: mdrons@drons.com
> To: Perrin Harkins <pe...@elem.com>,
>      darren chamberlain <dl...@users.sourceforge.net>
> Cc: modperl@perl.apache.org
> Subject: Re: variables not changing with modperl??
>
> Sorry,  There is a my in front of the ($cookie,$user)
> code.  I am confused about your second statement about
> parsing the input.  What should I be doing?  Do you
> mean I should use $r->read($content,
> $r->header_in('Content-length'))? to read in the
> variables?

Michael,

In one of your posts, you included the following code:

#!/usr/bin/perl -wT
use strict;
print "<body>";
my $r = Apache->request;
$r->content_type("text/html");
$r->status(200);
my $auth_type = $r->auth_type;
$cookie=$auth_type->key;
($user,$hash)=split(/:/,$cookie);
read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'});
my @pairs = split(/&/, $buffer);
foreach my $pair (@pairs) {
....
}

There is no "my" in front of $cookie or $user variables.  If you are
lexically scoping them somewhere else in your script, please include
enough of an example for us to help you.

> I use the AuthCookie modules to set the cookies and to
> authenicate.

As for this and Perrin's comment about parsing on your own, the point
is that you've written a lot of code that has already been written and
debugged by a lot of really smart people.  There's no reason for you
to be reading STDIN and spliting and all that.  If you're using
mod_perl, then you really should do yourself a favor and read up on
Apache::Request and Apache::Cookie for doing all this stuff.  It's
much faster (it's written in c), safer (it's been debugged), easier
(just install the module), and standard (i.e., more maintainable).

Of course, some people say that the nice thing about standards is that
there are so many to choose from.  :)

ky


Re: variables not changing with modperl??

Posted by Michael Drons <md...@yahoo.com>.
Sorry,  There is a my in front of the ($cookie,$user)
code.  I am confused about your second statement about
parsing the input.  What should I be doing?  Do you
mean I should use $r->read($content,
$r->header_in('Content-length'))? to read in the
variables?  

I use the AuthCookie modules to set the cookies and to
authenicate.

Mike


--- Perrin Harkins <pe...@elem.com> wrote:
> darren chamberlain wrote:
> > Make those global symbols ($cookie, $user, and
> $hash) lexical (declare
> > with my) and the code will both compile and do
> what you expect (i.e.,
> > not maintain values from call to call).
> 
> That's what I was thinking too.  Also, it's really
> not a good idea to do 
> your own parsing of the input and cookies.  You
> should use one of the 
> CGI modules or Apache::Request and Apache::Cookie
> for this.  Writing 
> your own routine for this is just asking for
> security problems and bugs.
> 
> - Perrin
> 
> 


__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

Re: variables not changing with modperl??

Posted by Perrin Harkins <pe...@elem.com>.
darren chamberlain wrote:
> Make those global symbols ($cookie, $user, and $hash) lexical (declare
> with my) and the code will both compile and do what you expect (i.e.,
> not maintain values from call to call).

That's what I was thinking too.  Also, it's really not a good idea to do 
your own parsing of the input and cookies.  You should use one of the 
CGI modules or Apache::Request and Apache::Cookie for this.  Writing 
your own routine for this is just asking for security problems and bugs.

- Perrin



Re: variables not changing with modperl??

Posted by Paul Simon <wr...@yahoo.com>.
Hi darren

Did you try starting apache with "httpd -X".  It spawns only one process and that helps keep things in order, as far as variable values.  You can try trouble shooting with that.

 darren chamberlain wrote:* Michael Drons [2002-08-13 01:55]:
> Thanks for the link. I actually don't use functions.
> Everything is mostly in MAIN. Here is a snip of code:
>
> #!/usr/bin/perl -wT
> use strict;
> print "";
> my $r = Apache->request;
> $r->content_type("text/html");
> $r->status(200);
> my $auth_type = $r->auth_type;
> $cookie=$auth_type->key;
> ($user,$hash)=split(/:/,$cookie);
> read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'});
> my @pairs = split(/&/, $buffer);
> foreach my $pair (@pairs) {
> ....
> }
>
> What I am doing wrong? Everytime the script runs the
> values of the variables coming in change. Should I
> use the delete function and delete all of the
> variables at the end of the script? @pairs is what
> should change, but sometimes does not. I have tried
> to add a undef @pairs before the split, but no luck.

Are you sure that this is the code that is running? It doesn't compile:

$ perl
#!/usr/bin/perl -wT
use strict;
print "";
my $r = Apache->request;
$r->content_type("text/html");
$r->status(200);
my $auth_type = $r->auth_type;
$cookie=$auth_type->key;
($user,$hash)=split(/:/,$cookie);
read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'});
my @pairs = split(/&/, $buffer);
foreach my $pair (@pairs) { }
Global symbol "$cookie" requires explicit package name at - line 7.
Global symbol "$user" requires explicit package name at - line 8.
Global symbol "$hash" requires explicit package name at - line 8.
Global symbol "$cookie" requires explicit package name at - line 8.
Execution of - aborted due to compilation errors

Make those global symbols ($cookie, $user, and $hash) lexical (declare
with my) and the code will both compile and do what you expect (i.e.,
not maintain values from call to call).

You'll also want to print things *after* you set the content type and
status, not before.

(darren)

--
The biggest difference between time and space is that you can't
reuse time.
-- Merrick Furst


---------------------------------
Do You Yahoo!?
HotJobs, a Yahoo! service - Search Thousands of New Jobs

Re: variables not changing with modperl??

Posted by darren chamberlain <dl...@users.sourceforge.net>.
* Michael Drons <md...@yahoo.com> [2002-08-13 01:55]:
> Thanks for the link.  I actually don't use functions.
> Everything is mostly in MAIN.  Here is a snip of code:
>
> #!/usr/bin/perl -wT
> use strict;
> print "<body>";
> my $r = Apache->request;
> $r->content_type("text/html");
> $r->status(200);
> my $auth_type = $r->auth_type;
> $cookie=$auth_type->key;
> ($user,$hash)=split(/:/,$cookie);
> read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'});
> my @pairs = split(/&/, $buffer);
> foreach my $pair (@pairs) {
> ....
> }
>
> What I am doing wrong?  Everytime the script runs the
> values of the variables coming in change.  Should I
> use the delete function and delete all of the
> variables at the end of the script?  @pairs is what
> should change, but sometimes does not.  I have tried
> to add a undef @pairs before the split, but no luck.

Are you sure that this is the code that is running?  It doesn't compile:

  $ perl
  #!/usr/bin/perl -wT
  use strict;
  print "<body>";
  my $r = Apache->request;
  $r->content_type("text/html");
  $r->status(200);
  my $auth_type = $r->auth_type;
  $cookie=$auth_type->key;
  ($user,$hash)=split(/:/,$cookie);
  read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'});
  my @pairs = split(/&/, $buffer);
  foreach my $pair (@pairs) { }
  Global symbol "$cookie" requires explicit package name at - line 7.
  Global symbol "$user" requires explicit package name at - line 8.
  Global symbol "$hash" requires explicit package name at - line 8.
  Global symbol "$cookie" requires explicit package name at - line 8.
  Execution of - aborted due to compilation errors

Make those global symbols ($cookie, $user, and $hash) lexical (declare
with my) and the code will both compile and do what you expect (i.e.,
not maintain values from call to call).

You'll also want to print things *after* you set the content type and
status, not before.

(darren)

--
The biggest difference between time and space is that you can't
reuse time.
    -- Merrick Furst

Re: variables not changing with modperl??

Posted by Michael Drons <md...@yahoo.com>.
Thanks for the link.  I actually don't use functions. 
Everything is mostly in MAIN.  Here is a snip of code:

#!/usr/bin/perl -wT
use strict;
print "<body>";
my $r = Apache->request;
$r->content_type("text/html");
$r->status(200);
my $auth_type = $r->auth_type;
$cookie=$auth_type->key;
($user,$hash)=split(/:/,$cookie);
read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'});
my @pairs = split(/&/, $buffer);
foreach my $pair (@pairs) {
....
}

What I am doing wrong?  Everytime the script runs the
values of the variables coming in change.  Should I
use the delete function and delete all of the
variables at the end of the script?  @pairs is what
should change, but sometimes does not.  I have tried
to add a undef @pairs before the split, but no luck.


Mike

--- Perrin Harkins <pe...@elem.com> wrote:
> Michael Drons wrote:
> > I am using Apache::Registry (Apache 1.3.26) I am
> see
> > weird things happen with my scripts.  I have have
> "use
> > strict" in all of the scripts and I use my() for
> all
> > of my variables.  But I still have variables that
> > contain data from previous loads.
> 
> Sounds like the closure problem with subroutines in
> Apache::Registry. 
> Does you code have subroutines that refer to
> variables declared outside 
> of them?
> 
> > Everything I can find in docs says read the FAQ at
> 
> > http://perl.apache.org/faq/, which does not
> exists.
> 
> Read this:
>
http://perl.apache.org/docs/general/perl_reference/perl_reference.html#my___Scoped_Variable_in_Nested_Subroutines
> 
> - Perrin
> 


__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

Re: variables not changing with modperl??

Posted by Perrin Harkins <pe...@elem.com>.
Michael Drons wrote:
> I am using Apache::Registry (Apache 1.3.26) I am see
> weird things happen with my scripts.  I have have "use
> strict" in all of the scripts and I use my() for all
> of my variables.  But I still have variables that
> contain data from previous loads.

Sounds like the closure problem with subroutines in Apache::Registry. 
Does you code have subroutines that refer to variables declared outside 
of them?

> Everything I can find in docs says read the FAQ at 
> http://perl.apache.org/faq/, which does not exists.

Read this:
http://perl.apache.org/docs/general/perl_reference/perl_reference.html#my___Scoped_Variable_in_Nested_Subroutines

- Perrin