I read the FAQs, tried several searches, either the info I need isn't here yet or more likely I am not familiar enough with MonkSearching to find it. If this should be posted somewhere else, or there is already a thread that will help me, please let me know.
I am a windows programmer teaching himself perl/cgi using some unix server or another supplied by my ISP that appears to run Apache. The stupidity inherent in that last sentence is only a warning of the vast ignorance yet to come...
The goal of this script is to read a data file that consists of name=value pairs and compare it to the name=value pairs coming in (as stdin) from my entry web page. When a name from the web page matches a name from the data file and there is a value attached to the name, overwrite the data file value with the web page value. Once all name=value pairs have been inspected, write the modified data file back to current directory, and refresh the entry web page (restoring all the form controls to blank).
Problems/symptoms:
Invoking the script from the unix command line works perfectly, data file is modified and re-written, the other script referenced at the end of the main script is executed, eveyone is happy.
Invoking the script as a form submit action from the web page yields different results. The data comparison does occur - as evidenced by the "print" output at the end of the script which shows the modified data file - but nothing writes back to the data file. Also, the "system" call doesn't actually do anything (nor did the "exec" call I had there originally).
Is there a limit of one output device per CGI script or something? It sounds stupid to even type that question but I can't think of another reason. If I comment out the web page refresh code, then the data file works properly - although the exec calls still do not work.
Here is the script that has been killing me for two days:
#!/usr/local/bin/perl
my $in ;
my @dataArray ;
my $dataElement ;
my $dataCtr ;
my $outLine ;
my $wkStr ;
#
# ------------ read standard in
#
if ( $ENV{'REQUEST_METHOD'} eq "GET" )
{
$in = $ENV{'QUERY_STRING'} ;
}
else
{
$in = <STDIN> ;
}
#
# ------------ strip out bad stuff
#
$in =~ s/%(..)/pack("c",hex($1))/ge ;
$in =~ s/\+/ /g ;
#
# ------------ add extra row delimiter to
# ------------ make delimiter-based looping easier
#
$in .= "&" ;
#
# ----------- get character data file
#
open ( DATA, "character.data" ) ;
@dataArray = <DATA> ;
$dataCtr = $#dataArray ;
close ( DATA ) ;
#
# ----------- update data file from stdn
#
#### Walk through data array.
#### for each entry,
#### find matching tag in $in (if any)
#### if match, overwrite data value
#### with stdin value
#### otherwise write data value
for ( $d = 0; $d <= $dataCtr; $d++ )
{
$hitPos = 0 ;
$endPos = 0 ;
$wkStr = "" ;
@dataElement = split ( /=/, $dataArray[$d] ) ;
$searchStr = @dataElement[0] . "=" ;
$hitPos = index ( $in, $searchStr ) ;
$wkStr = @dataElement[1] ;
if ( $hitPos >= 0 )
{
$hitPos += length ( $searchStr ) ;
$endPos = index ( $in, "&", $hitPos ) ;
if ( $endPos > $hitPos )
{
$wkStr = substr ( $in, $hitPos, $endPos - $hitPos ) ;
}
}
#
# ----------- for some reason newlines are not consistantly present
#
if ( index ( $wkStr, "\n" ) > 0 )
{
$outLine .= $searchStr . $wkStr ;
}
else
{
$outLine .= $searchStr . $wkStr . "\n" ;
}
}
#
# ----------- write to output data file
#
open ( OUTFILE, ">character.data" );
select ( OUTFILE );
print <<END;
$outLine
END
close ( OUTFILE );
#
# ----------- update menu html
#
system 'menu.cgi' ;
#
# ----------- write to browser
#
open ( ENTRYPAGE, "save.htm" ) ;
@EntryPage = <ENTRYPAGE> ;
close ( ENTRYPAGE ) ;
select ( STDOUT ) ;
print <<END ;
Content-Type: text/html\n\n
@EntryPage
$outLine
END
Edit kudra,
2002-04-19
Changed title
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.