Writing to file
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
Moc posted this at 08:14 — 23rd December 2000.
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);
http://www.mocdesigns.com - In the works.
japhy posted this at 14:08 — 23rd December 2000.
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,, and to let the file lock on it disappear once the filehandle is closed.
Moc posted this at 18:41 — 23rd December 2000.
They have: 11 posts
Joined: Dec 2000
Please explain.
http://www.mocdesigns.com - In the works.
japhy posted this at 21:07 — 23rd December 2000.
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, 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.
Moc posted this at 22:47 — 23rd December 2000.
They have: 11 posts
Joined: Dec 2000
thanks for the information japhy.
KoRnholio posted this at 01:31 — 24th December 2000.
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
-------
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.