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


In reply to perl/cgi question: script works from unix command line, but not web page by geoffhanna

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.