Need help writing a basic script print to text file

C

chewdon

I am trying to write a cgi script that will print text to a txt file
with the name of the txt file being the date and time it was created.
I am new to this and the only script I have been able to write is
Hello World!. From what I have found on the internet I tried to write
this but of course it does not work. This is what I have written.

#!/usr/local/bin/perl

#sends user back to my home page

Location: "http://www.mywebsite.com/index.html

print "text file data"; >> textfile.txt

This seems like it should be a pretty simple script but I can not
figure it out. Can anybody please tell me how to create this script?

Thanks
 
J

J. Gleixner

chewdon said:
I am trying to write a cgi script that will print text to a txt file
with the name of the txt file being the date and time it was created.
I am new to this and the only script I have been able to write is
Hello World!. From what I have found on the internet I tried to write
this but of course it does not work. This is what I have written.

#!/usr/local/bin/perl

#sends user back to my home page

Location: "http://www.mywebsite.com/index.html

print "text file data"; >> textfile.txt

This seems like it should be a pretty simple script but I can not
figure it out. Can anybody please tell me how to create this script?

To learn how to open and write to a file, you can read your
local documentation that comes with perl:

perldoc -f open

There are a lot of documents online for CGI and perl. Typically
you'd use the CGI module:

http://search.cpan.org/~lds/CGI.pm-3.29/CGI.pm
 
H

Henry Law

chewdon said:
I am trying to write a cgi script that will print text to a txt file
with the name of the txt file being the date and time it was created.
I am new to this and the only script I have been able to write is
Hello World!. From what I have found on the internet I tried to write
this but of course it does not work. This is what I have written.

#!/usr/local/bin/perl

#sends user back to my home page

Location: "http://www.mywebsite.com/index.html

print "text file data"; >> textfile.txt

That's not what you're trying to run; it isn't syntactically correct
Perl, nor any other language with which I'm familiar. Many things will
make it harder for people to help you: that's one of them. Always copy
and paste in your real code.

That said I'm not even sure what you want to do. Some Perl statements
which will write text to a named file would include

open THATFILE, '>', "namedfile" or die "Open failed:$!";
print THATFILE "stuff\n";

But instead of "namedfile" you want something like "yyyy-mm-dd-hh-mm-ss"
and to get that you need the "localtime" and "time" commands; look them
up in your Perl help and have a go at writing the code.

Write a small program that works out your file's name, based on the date
and time, and opens it for writing. Don't worry with CGI for now. When
you've got the thing working, or if you can't and need help, post again.
 
B

Bill H

I am trying to write a cgi script that will print text to a txt file
with the name of the txt file being the date and time it was created.
I am new to this and the only script I have been able to write is
Hello World!. From what I have found on the internet I tried to write
this but of course it does not work. This is what I have written.

#!/usr/local/bin/perl

#sends user back to my home page

Location: "http://www.mywebsite.com/index.html

print "text file data"; >> textfile.txt

This seems like it should be a pretty simple script but I can not
figure it out. Can anybody please tell me how to create this script?

Thanks

Heres a brute force example:

#!/usr/local/bin/perl

$filename = time();
$filename += ".txt";
open(FILE,">$filename");
print FILE "any and all text you want to write\n";
close(FILE);

print "Location: http://www.mywebsite.com/index.html\n\n";
exit;

It's missing a bunch of things, flock, die etc - but brute force will
create a file that is named for the complete time (actually number of
seconds since the epoch).

Bill H
 
B

Bill H

Yes, really very brute :)

$filename += ".txt";
should be
$filename .= ".txt";
or simple
$filename = time . '.txt';

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

You are right Petr. Thats what I get for typing code I cant test :)

Bill H
 
B

Brian McCauley

[ A Perl4 script ]
It's missing a bunch of things,

Yes, sure is.

Showing newbies how to write for Perl4 isn't really a very nice thing
to do. No point learning stuff you'll have to unlearn.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top