Home > Archive > PERL CGI Beginners > May 2004 > cgi retaining state (even though I don't want it to)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
cgi retaining state (even though I don't want it to)
|
|
| John Kennedy 2004-05-22, 11:31 am |
| Hi,
I am a bit of a beginner and this is my first time using a mailing list so
please go easy with me.
I have a problem with a perl/cgi script I have written.
It's a simple script that takes 2 values and parses an xml file for all
entries which lie between the two. It then takes the answers and posts a
table back so you can see what lies within your specified window.
I use
use strict;
use XML::DOM;
use CGI qw/:standard -nosticky/;
The script works fine from the command line when debugging and also works
fine when I test it from my browser.
The problem arrises when I try a second set of values. The script seems to
remember the original values and sticks with them BUT the parse of the xml
file is messed up. Basically if I submit a second request the error which
I output tells me that no entries were found and details the window to be
the previous window not the one defined by the new values I use..
I am really about this.
To make things worse if I wait a little and click reload with the error
still present on my browser window then it correctly uses the new values
and correctly parses the xml file..
The problem seems time dependent. If I wait for 5 seconds after I submit a
request and then submit a new one I get the correct behaviour.(sometimes
this takes longer to work).
The cgi script seems to be saving the final state of my previous request
and I don't know how to stop this..
can anybody help??
cheers
johnk
--
Dr. John A Kennedy email: John.Kennedy@cern.ch
LMU Muenchen
Sektion Physik
Am Coulombwall 1 Phone: 0049(89)2891 4152
D 85748 Garching, Germany Fax: 0049(89)2891 4103
| |
| Zentara 2004-05-22, 11:31 am |
| On Fri, 16 Apr 2004 12:31:34 +0100 (GMT), John.Kennedy@cern.ch (John
Kennedy) wrote:
>Hi,
>
>I am a bit of a beginner and this is my first time using a mailing list so
>please go easy with me.
>
>I have a problem with a perl/cgi script I have written.
>
>It's a simple script that takes 2 values and parses an xml file for all
>entries which lie between the two. It then takes the answers and posts a
>table back so you can see what lies within your specified window.
>
>I use
>
>use strict;
>use XML::DOM;
>use CGI qw/:standard -nosticky/;
>
>The script works fine from the command line when debugging and also works
>fine when I test it from my browser.
>
>The problem arrises when I try a second set of values. The script seems to
>remember the original values and sticks with them BUT the parse of the xml
>file is messed up. Basically if I submit a second request the error which
>I output tells me that no entries were found and details the window to be
>the previous window not the one defined by the new values I use..
Are you using modperl and are unaware of it? modperl will keep
values around.
>I am really about this.
>
>To make things worse if I wait a little and click reload with the error
>still present on my browser window then it correctly uses the new values
>and correctly parses the xml file..
>
>The problem seems time dependent. If I wait for 5 seconds after I submit a
>request and then submit a new one I get the correct behaviour.(sometimes
>this takes longer to work).
>
>The cgi script seems to be saving the final state of my previous request
>and I don't know how to stop this..
>
>can anybody help??
You should post the code so people can look at it.
Some wild ideas which pop into mind.
-- a weird flocking problem with the XML file ?
-- some sort of cacheing problem keeping temp files laying around ?
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
| |
| John Kennedy 2004-05-22, 11:32 am |
| Hi,
just posting my code as requested by zentara
cheers
johnk
#!/usr/bin/perl -wT
#
#
use strict;
use XML::DOM;
use CGI qw/:standard -nosticky/;
my $q = new CGI;
my $minMass = $q->param( "minmass" ) || error( $q, "No Min Mass entered." );
my $maxMass = $q->param( "maxmass" ) || error( $q, "No Max Mass entered." );
if ($maxMass <= $minMass){
error ( $q, "Invalid Mass Window entered." );
}
my @tableData;
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile("particles.xml");
my @particles = $doc->getElementsByTagName("Particle");
foreach my $particle (@particles){
my @melement = $particle->getElementsByTagName("Mass");
my @qualities = $melement[0]->getChildNodes;
foreach my $quality (@qualities){
my $thisMass = $quality->toString if ($quality->getNodeType eq TEXT_NODE);
if ($thisMass >= $minMass && $thisMass <= $maxMass){
&writeout($particle);
}
}
}
sub writeout{
my $particle = shift;
my $outName = &getValue($particle,"Name");
my $outMass = &getValue($particle,"Mass");
my $outWidth = &getValue($particle,"Width");
my $outCharges = &getValue($particle,"Charges");
push @tableData,& #91;$outName,$outMass,$outWidth,$outChar
ges];
}
sub getValue{
my ($particle,$tag) = @_;
my @elements = $particle->getElementsByTagName($tag);
if (!@elements){return "N/A"};
my @qualities = $elements[0]->getChildNodes;
foreach my $quality (@qualities){
my $thisValue = $quality->toString if ($quality->getNodeType eq TEXT_NODE);
return $thisValue;
}
}
my $len = scalar(@tableData);
print $q->header(-type=>'text/html',
-expires=>'now');
print $q->header("Pragma: no-cache");
print "<HTML>\n";
if ($len == 0 ) {
error( $q, "No particles found in mass window." );
}else{
print "<TABLE ALIGN=CENTER WIDTH=\"80\%\" CELLSPACING=2
CELLPADDING=2 BORDER=2 BGCOLOR=LIGHTBLUE>\n";
print "<TR>\n";
print "<TD>Name</TD>\n";
print "<TD>Mass</TD>\n";
print "<TD>Width</TD>\n";
print "<TD>Charges</TD>\n";
for (my $i=0; $i<$len; $i++){
print "<TR>\n";
print "<TD>$tableData[$i]->[0]</TD>\n";
print "<TD>$tableData[$i]->[1]</TD>\n";
print "<TD>$tableData[$i]->[2]</TD>\n";
print "<TD>$tableData[$i]->[3]</TD>\n";
print "</TR>\n";
}
print "<TABLE>\n";
}
print "</HTML>\n";
$doc->dispose;
$q->delete_all;
exit;
sub error {
my( $q, $reason ) = @_;
print $q->header(-type=>'text/html',
-expires=>'now'),
$q->start_html( "Error" ),
$q->h1( "Error" ),
$q->p( "Your request was not procesed because the following error ",
"occured: " ),
$q->p( $q->i( $reason ) );
print "$minMass : $maxMass\n";
$q->end_html;
exit;
}
On Sat, 17 Apr 2004, zentara wrote:
> On Fri, 16 Apr 2004 12:31:34 +0100 (GMT), John.Kennedy@cern.ch (John
> Kennedy) wrote:
>
>
> Are you using modperl and are unaware of it? modperl will keep
> values around.
>
>
> You should post the code so people can look at it.
> Some wild ideas which pop into mind.
>
> -- a weird flocking problem with the XML file ?
> -- some sort of cacheing problem keeping temp files laying around ?
>
>
>
>
>
--
Dr. John A Kennedy email: John.Kennedy@cern.ch
LMU Muenchen
Sektion Physik
Am Coulombwall 1 Phone: 0049(89)2891 4152
D 85748 Garching, Germany Fax: 0049(89)2891 4103
| |
| Zentara 2004-05-22, 11:32 am |
| On Mon, 19 Apr 2004 09:40:04 +0100 (GMT), John.Kennedy@cern.ch (John
Kennedy) wrote:
>Hi,
>
>just posting my code as requested by zentara
Well, that code looks pretty clean to me, I would be perplexed too.
It would seem there are 2 possible places for the problem, which may
cause overlap between 2 different cgi accesses.
1. A flocking problem on the XML file being accessed by the
XML::DOM::Parser. Maybe the module is so slow, that it takes 5
seconds to complete, and in the meantime, if you try to access it a
second time, without flocking the XML file, you get the odd results.
Or the module is doing some kind of cacheing?
Flocking problems can produce totally unexpected results.
2. (A long shot), you are running this script from a directory on a
server. that has been setup in the apache configuration file to use
Apache Registry. It might be worth a shot to run this script to
see what you have going:
use Apache::Registry;
print "Content-type: text/plain\n\n";
foreach $var (sort(keys(%ENV))) {
$val = $ENV{$var};
$val =~ s|\n|\\n|g;
$val =~ s|"|\\"|g;
print "${var}=\"${val}\"\n";
}
########################################
################
Here is another script you might try to see what kind of cacheing is
going on. By Aristotle of perlmonks.org
Description:
This is really more of a HTTP- than Perl-related post; the following
snippet emits all of the currently specified HTTP headers that may
affect caching, with values set to coerce clients into not caching
the
document in question. It's useful for debugging as well as generated
pages that actually are very dynamic, but shouldn't be used blindly
for everything - caching is there for a reason.
(I'm posting this here mainly so as to have a node to refer people to
as needed.)
use CGI qw(:standard);
use POSIX qw(strftime);
print header(
# date in the past
-expires => 'Sat, 26 Jul 1997 05:00:00 GMT',
# always modified
-Last_Modified => strftime('%a, %d %b %Y %H:%M:%S GMT',
gmtime),
# HTTP/1.0
-Pragma => 'no-cache',
# HTTP/1.1
-Cache_Control => join(', ', qw(
no-store
no-cache
must-revalidate
post-check=0
pre-check=0
)),
);
__END__
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
| |
| Charles K. Clarkson 2004-05-22, 11:32 am |
| John Kennedy <John.Kennedy@cern.ch> wrote:
:
: just posting my code as requested by zentara
Stop top-posting.
: #!/usr/bin/perl -wT
: #
: #
:
: use strict;
: use XML::DOM;
: use CGI qw/:standard -nosticky/;
:
: my $q = new CGI;
[snipped helpful code -- Thanks.]
Two items.
1. You don't need to install the ":standard" subroutines
(more than 100 of them) to use CGI.pm as an object.
2. The "-nosticky" pragma doesn't do anything if you are
not creating the form in this script.
What is generating the form?
It's not being generated from this script. Changing this
script will not affect the form.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
|
|
|
|
|