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

Hi Monks, I am stuck in building a CGI program, which read/write to a file. I am able to do the tasks separately. Here is how I am writing to the file.
# now write to the file open(OUT, ">/u001/data/slatrk/data/comments.dat"); foreach my $p (param()) { print OUT param($p) . "\n"; } print OUT "\n"; close(OUT);
And this is how I am reading from the file.(Code snippet)
# Extraction Process sub first_process { $data_dir="/u001/data/slatrk/data"; open(F1,"<$data_dir/comment_job.config") || die "Cannot open file1 +:$!\n"; open(F2,"<$data_dir/comments.dat") || die "Cannot open file2:$!\n" +; while(<F1>){ chomp; chomp(my $f2=<F2>); push (@comment, $_ . "|" . $f2 . "\n"); } } push(@rows,$q->th(['APPLICATION', 'COMMENT'])); &first_process; foreach (@comment) { #array starts here ($jobname, $comment)= split /\|/; push(@rows,$q->td([$jobname, '<textarea>'.$comment.'</textarea +>'])); }#array ends here print qq {<div id='container'>}; print $q->table( {-cellpadding => 3}, Tr(\@rows) );
Now I want to combine the two methods and I can write the data to the data using param. It should be in a loop so that i don't have to give textarea names separately. Can someone help me in this regard.

Replies are listed 'Best First'.
Re: read/write data in using CGI
by GrandFather (Saint) on May 29, 2014 at 11:04 UTC

    Why? Your code doesn't hint at what you are trying to achieve overall and the answer you want depends on just what you want to do.

    An answer may be that a normal CGI application accepts input on stdin and spits output to stdout so one approach would be to simply redirect stdin and stdio to your files. That would work fine for testing some CGI code you've written or preparing dynamic HTML offline, but maybe you have a different reason for doing this?

    Perl is the programming world's equivalent of English
      I want the current file content to be shown up in the textarea, so that I can change it and on submit it should write the updated stuffs in the same file. I believe i can't do it using stdin/stdout.
Re: read/write data in using CGI
by Anonymous Monk on May 29, 2014 at 14:07 UTC
    in this line add a random number generator to textarea name. So that param can take different values and it should work.
    push(@rows,$q->td([$jobname, '<textarea name="$random_number">'.$comm +ent.'</textarea +>']));