Writing to file

They have: 3 posts

Joined: Dec 2000

I've done quite a bit of Perl CGI script installing, and thus know some of the basics of it, and even know a bit of Perl, of course that is limited to the print function and simple math operators. And, without buying a book, there's one thing I need to know: Simple writing to a file.

Say there's one simple textarea input named 'stuff'. I want to write to a .txt(precreated), it's full path being /www/kain/blah.txt, and the script itself(if that matters) being in /www/kain/cgi-bin/wow.cgi.

What would the main body of the script(after the Perl interpreter path declaration) be? This is barebones, and I can't seem to find information on this without shelling out 30 dollars for a book on Perl, online tutorials only seem to cover mathematical functions and stuff.

-------
Stuff

They have: 11 posts

Joined: Dec 2000

I think this is what you are asking so here goes. You need to have some routine read in the information passed by your form.

Which then you do the following :

# notice that the formdata is hash that is from the routine
# which parses the information submitted in the form.

$stuff = $formdata{'stuff'};

# This opens blah.txt and writes to the file, this
# overwrites the file if it has anything. If you don't want
# to overwrite the file and simply just append to the file
# use >> instead of the > below

open(LOG, ">/www/kain/blah.txt");
flock(LOG, 2);
print LOG "$stuff\n";
flock(LOG, 8);
close(LOG);

They have: 161 posts

Joined: Dec 1999

You don't need to buy a book to tell you how to use Perl's functions -- read the documentation that comes with Perl, available via the perldoc program, or online at http://www.perldoc.com/.

And Moc, it's highly advisable to NOT call flock(FH,Cool, and to let the file lock on it disappear once the filehandle is closed.

They have: 11 posts

Joined: Dec 2000

Please explain.

Quote: Originally posted by japhy
And Moc, it's highly advisable to NOT call [b]flock(FH,Cool, and to let the file lock on it disappear once the filehandle is closed. [/B]

They have: 161 posts

Joined: Dec 1999

When you close() a filehandle, its output buffer is flushed, and anything you've print()ed to the filehandle and hasn't yet been stored in the file is stored. close()ing a filehandle also kills any file locks on it, since the filehandle can't be locked if it's not around.

Calling flock(FH,Cool before close(FH) thus allows something to modify the file IN BETWEEN THE TWO operations, and may end up messing up all the work you've done, since not everything you've asked to print to the file is necessarily there.

They have: 11 posts

Joined: Dec 2000

thanks for the information japhy.

They have: 3 posts

Joined: Dec 2000

Thanks Moc, it seems easier than I thought. I usually learn well from templates like that(learned C that way), so I can probably actually write programs now Smiling

-------
Stuff

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.