Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am a beginning programmer, so this may a really easy question. I wrote a perlscript to write information based on the query string, to a file(Which works). Then I wrote another script to read that file and print the string. The write program works, and it does write to the file. But, the Read script work once, (When I upload it, or update it), then it will keep displaying the same string over and over, when I change the string (Through the write script). The read script doesnt update (By the way, the file(s) are Chmod to 777)! Here is the code for both programs:
#!/usr/bin/perl # Write Script ($a, $b) = split('=', $ENV{'QUERY_STRING'}); $b =~ tr/+/ /; $b =~ s/%(..)/pack("C", hex($1))/eg; open(FILE, ">wrfile.txt"); print FILE $b; close( FILE ); ---------------------------------------- #!/usr/bin/perl # Read Script (Doesn't print updated strings) open(FILE, "<wrfile.txt"); while(<FILE>){ print "Content-type: text/html\n\n"; print $_; } close(FILE);

Replies are listed 'Best First'.
Re: File I/O Help
by swiftone (Curate) on Jun 06, 2000 at 19:36 UTC
    Though this is a simple script, I'd recommend getting in the habit of using CGI.pm. Small scripts have a habit of growing, and if you are new to Perl, best to start by making life easier on yourself.
Re: File I/O Help
by Corion (Patriarch) on Jun 06, 2000 at 17:45 UTC

    I think one problem is, that you are repeatedly printing your "Content-type:" header - move it before the while() loop. Maybe the problem disappears when using another browser ? Did you look at the source code that your script sends you back ?

Re: File I/O Help
by lhoward (Vicar) on Jun 06, 2000 at 18:20 UTC
    Whenever you do file-io from CGI scripts you can never trust relative paths. Try putting the full-path to where you want wrfile.txt to be in both programs.

    Also you should check the error returns from the "open" in both scripts or you could miss errors.

Re: File I/O Help
by chromatic (Archbishop) on Jun 07, 2000 at 02:50 UTC
    Let's change the Read Script a little bit:
    open(FILE, "<wrfile.txt") || die "Can't open file for reading: $!"; print "Content-type: text/html\n\n"; # binmode FILE; print while <FILE>; close(FILE);
    This way, we only print out the mimetype header once per file, and it's more Perlish this way. You might use binmode if you're going to serve something other than plain text files on a Unix OS. (Hey, be flexible!) Also, this has some error checking.