473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Cannot write from perl CGI to text file

Hi all,

I am new to perl and this forum. I am trying to setup a mailing list subscription functionality for customers to receive a periodic newsletter from me. My perl program grabs the html form 'email address' text input but I am then having issues writing this data to a plain text file.

Below is my html form followed by my perl script. When i run this on IIS, i dont receive any errors, nor does anything get written to the file. Any help would be most appreciated. :)

email.html
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <FORM ACTION="/new/email.pl" METHOD="POST">
  4. Email Address: <INPUT TYPE="text" NAME="email" size=30>
  5. <br><br><INPUT TYPE="submit" VALUE="Sign up">
  6. </FORM>
  7. </body>
  8. </html>
  9.  
email.pl
Expand|Select|Wrap|Line Numbers
  1. print "Content-type: text/html\n\n";
  2.  
  3. #Read email address from Form
  4. if ($ENV{'REQUEST_METHOD'} eq 'POST')
  5.  {
  6. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  7. @pairs = split(/&/, $buffer);
  8. foreach $pair (@pairs)
                  {
  9.        ($name, $value) = split(/=/, $pair);
  10.        $value =~ tr/+/ /;
  11.        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  12.         $FORM{$name} = $value;
  13.     }}  
  14.  
  15. #Write to file
  16. my $file = '/new/email.txt';  
  17.  
  18. open (FILE, ">>" . $file) or die "cannot open file for appending: $!"; 
  19. flock (FILE, 2) or die "cannot lock file exclusively: $!"; 
  20. print FILE $FORM{email};
  21. close (FILE) or die "cannot close file: $!";   
  22.  
Thanks again,

Mick
Oct 15 '07 #1
21 5771
hi,
try with this simple code,
definitely it will work 4 u,

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <FORM ACTION="/new/email.pl" METHOD="POST">
  4. Email Address: <INPUT TYPE="text" NAME="email" size=30>
  5. <br><br><INPUT TYPE="submit" VALUE="Sign up">
  6. </FORM>
  7. </body>
  8. </html>
  9.  
email.pl
-----------
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI qw(:standard);
  3.  use Fcntl ':flock';
  4. print "Content-type: text/html\n\n";
  5. $a=param('email');
  6. #Write to file
  7. my $file = '/new/email.txt'; 
  8.  
  9. open (FILE, ">>" .$file) or die "cannot open file for appending: $!";
  10. flock (FILE, 2) or die "cannot lock file exclusively: $!";
  11. print FILE $a;
  12. close (FILE) or die "cannot close file: $!";
  13.  
Oct 15 '07 #2
numberwhun
3,509 Expert Mod 2GB
You have posted code into the forum without using proper code tags. It is best practice here on TSDN to wrap all code posted into the forum in code tags.

Code tags start with [code] and end with [/code]. If you need an example other than this one, the please refer to the REPLY GUIDELINES (or POSTING GUIDELINES if you are starting a discussion) to the right of the Message window. You will find the examples and suggestions for posting there.

In addition, you can also add the language to your code tags. Here is an example:

[code=perl]
<some code>

[/code]

Please know that I have fixed your posts above to include the proper code tags. Please be sure and use code tags in all of your future posts here on TSDN.

This applies to BOTH of you as neither of you used the tags!!

- Moderator
Oct 15 '07 #3
thanks for your quick response!! unfortunately, this still did not work. :( i ran it, entered an email address and a blank page came up with the status 'done'. still no write was made to the file.

so my .htm form has an email address entered and submitted and the output is a blank page. not sure what is up here. any other suggestions?

im running ActivePerl-5.8.8.820-MSWin32-x86-274739.msi on windows 2003 with IIS 6. dont know if this helps at all.

also, what is html 4 strict? do i need to change anything here?

cheers,

Mick
Oct 15 '07 #4
eWish
971 Expert 512MB
Well, I would make sure that the path to the file is correct. Then add some code for debuging. I don't believe that flock works on windows. I believe this is unix only.


Here is a script that will help you. I have added some code to check the email address to make sure that is validates. No need for a security hole:)

This code uses Email::Valid
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. use CGI::Carp qw/fatalsToBrowser/;
  8. use Email::Valid;
  9.  
  10. my $q = CGI->new;
  11.  
  12. print $q->header; 
  13. print $q->start_html(-title =>'Mysite.com'); 
  14.  
  15. my $email_file = 'h:/path/to/file/email_list.txt';
  16.  
  17.     if (Email::Valid->address($q->param('email'))) {
  18.          &process_data($q->param('email'));
  19.     } else {
  20.         print 'Your email address does not appear to be valid';
  21.     }
  22.  
  23. sub process_data {
  24.  
  25.     my ($email_to_log) = @_;
  26.  
  27.     open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
  28.     print $EMAIL_LIST $email_to_log;
  29.     close $EMAIL_LIST;
  30.  
  31.     print 'Go check your file to make sure it wrote correctly OR add some code to do it for you';    
  32. }
  33.  
  34. print $q->end_html();
  35.  
  36. 1;
Oct 15 '07 #5
eWish
971 Expert 512MB
Modification to the code I posted.
Change:
Expand|Select|Wrap|Line Numbers
  1. print $EMAIL_LIST $email_to_log;
To:
Expand|Select|Wrap|Line Numbers
  1. print $EMAIL_LIST $email_to_log, "\n";
Otherwise all of your email address' will end up on the same line. Also, the code I posted does not take into account duplicates.
Oct 15 '07 #6
eWish
971 Expert 512MB
Make sure you are using the proper path to perl or else you will get a 5OO error.
Widnow's will be something like C:\perl\bin\perl.exe

This line can be changed to your site's title, just change the wording "Mysite.com".
Expand|Select|Wrap|Line Numbers
  1. print $q->start_html(-title =>'Mysite.com');
Oct 16 '07 #7
Hi,

Thanks again for your suggestion as you have been most helpful. I thought that in order to prevent any further pain for you :), I would post here my exact code and system set up as it currently stands to see whether you can identify any issues.

Setup

-Currently my server is Win2003 with IIS 6.0
-The 3 files email.pl, email.html and email.txt all reside in C:\cgi-bin
-My perl install is located in C:\perl\bin\perl.exe as you mentioned

My code is currently as follows:

email.html


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <FORM ACTION="/cgi-bin/email.pl" METHOD="POST">
  4. Email Address: <INPUT TYPE="text" NAME="email" size=30>
  5. <br><br><INPUT TYPE="submit" VALUE="Sign up">
  6. </FORM>
  7. </body>
  8. </html>
  9.  

email.pl


Expand|Select|Wrap|Line Numbers
  1. #! C:\perl\bin\perl.exe
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. use CGI::Carp qw/fatalsToBrowser/;
  8. use Email::Valid;
  9.  
  10. my $q = CGI->new;
  11.  
  12. print $q->header; 
  13. print $q->start_html(-title =>'mysite.com.au'); 
  14.  
  15. my $email_file = 'c:/cgi-bin/email.txt';
  16.  
  17.     if (Email::Valid->address($q->param('email'))) {
  18.        &process_data($q->param('email'));
  19.     } else {
  20.         print 'Your email address does not appear to be valid';
  21.     }
  22.  
  23. sub process_data {
  24.  
  25.     my ($email_to_log) = @_;
  26.  
  27.     open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
  28.     print $EMAIL_LIST $email_to_log;
  29.     close $EMAIL_LIST;
  30.  
  31.     print 'Go check your file to make sure it wrote correctly OR add some code to do it for you';   
  32. }
  33.  
  34. print $q->end_html();
  35.  
  36. 1;
  37.  
-Also, to use Email::Valid, do I need to install/cfg anything further than the standard perl.

-For the IIS web extensions, is it advisable to use the .exe or .dll?

Cheers,

Mick
Oct 16 '07 #8
eWish
971 Expert 512MB
You will need to install Email::Valid which can be done using PPM. I use XAMPP which is an Apache Web Server environment (for development only) on a windows machine. For me the path to perl is as I showed you above. I am not certain how it is setup on Win2003 with IIS 6.0.

Do you still get error when you run the script? If so, then maybe this link can help you.

Kevin
Oct 17 '07 #9
i think im getting closer now. your "Go check your file to make sure it wrote correctly......" line appears after i submit the email address. however still nothing written to file. when using the -w flag, i receive the following warning:

print() on closed filehandle $EMAIL_LIST

The warning indicates that the offending line of code is:

Expand|Select|Wrap|Line Numbers
  1. print $EMAIL_LIST $email_to_log;
  2.  
i have tried printing the html form input back to the screen which works correctly so it must be in the file write code??

thanks
mick
Oct 17 '07 #10
eWish
971 Expert 512MB
I tested the exactly code that you have posted and it worked for me. It work to the file correctly. To me it sounds like the path is not correct.

Make the following modifications to your code and see what happens. As far as the -w. That is not needed it has already been done with the use warnings.
Expand|Select|Wrap|Line Numbers
  1. print "$ENV{'DOCUMENT_ROOT'}/NEW_____email.txt\n";
  2. my $email_file = "$ENV{'DOCUMENT_ROOT'}/NEW_____email.txt";
Once you confirm the path the change it back.
Oct 17 '07 #11
yep, the path is correct. i am using an absolute reference in my .pl script below:

now the program writes '0' to file. anything i type in the textfield comes up as a zero in the txt file.

if i replace the below line
Expand|Select|Wrap|Line Numbers
  1. my $email_to_log = @_;
with
Expand|Select|Wrap|Line Numbers
  1. my $email_to_log = "test";
, i can get 'test' printed to the file.

Expand|Select|Wrap|Line Numbers
  1. #!C:\perl\bin
  2.  
  3. use strict;
  4. use warnings;
  5. use CGI;
  6. use CGI::Carp qw/fatalsToBrowser/;
  7.  
  8. my $q = CGI->new;
  9. print $q->header('text/html'); 
  10. print $q->start_html(-title =>'Website Name'); 
  11.  
  12. my $email_file = 'c:/cgi-bin/email.txt';
  13.  
  14. &process_data($q->param('email'));
  15.  
  16. sub process_data{
  17.     my $email_to_log = @_;
  18.     open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
  19.     print $EMAIL_LIST "$email_to_log\n";
  20.     close $EMAIL_LIST;
  21. }
  22.  
  23. print $q->end_html();
  24.  
  25. 1;
  26.  
i have just stripped the code write back so the email::validate content is removed however the above script does not receive compilation errors.

cheers,

mick
Oct 18 '07 #12
eWish
971 Expert 512MB
Change:
Expand|Select|Wrap|Line Numbers
  1. my $email_to_log = @_;
To:
Expand|Select|Wrap|Line Numbers
  1. my ($email_to_log) = @_;
The reason is without the () you are getting the number of elements in the array @_. With the () you are getting the contents of the element(s) of the array.
Oct 18 '07 #13
ok, yes that makes sense. however, if that is the case, should there not be a '1' entered into the file to indicate the one parameter (the email address) that was entered in the form?

i say this because when i remove the parethesis around that line you mention above, i receive a "email.pl: Use of uninitialized value in string at C:\cgi-bon\email.pl line 23" error. then a " " (space) is written to the file.
Oct 18 '07 #14
eWish
971 Expert 512MB
When I used the code from post #12 as is and used your form and I did get a 1 in the file. When I added the () like so:
Expand|Select|Wrap|Line Numbers
  1. my ($email_to_log)  = @_;
I got the value of $q->param('email') as expected.
Oct 18 '07 #15
ok, that's an interesting point then. using the same code, a 0 or " " gets written to file, depending on the parenthesis being present or not. This is quite confusing as i can:

A) Write the html form content (once captured) back to the screen and
B) As mentioned before, write text such as 'test' to the file.

My program isnt doing both though....picking up the form input and then writing that form input to file.

I cant for the life of me think what the problem may be!?
Oct 18 '07 #16
KevinADC
4,059 Expert 2GB
replace this:

Expand|Select|Wrap|Line Numbers
  1. sub process_data{
  2.     my $email_to_log = @_;
  3.     open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
  4.     print $EMAIL_LIST "$email_to_log\n";
  5.     close $EMAIL_LIST;
  6. }
  7.  
with:

Expand|Select|Wrap|Line Numbers
  1. sub process_data{
  2.     open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
  3.     print $EMAIL_LIST $q->param('email'), "\n";
  4.     close $EMAIL_LIST;
  5. }
and report back
Oct 18 '07 #17
hi KevinADC,

there are no compilation errors or warnings. however, when run nothing but a space gets printed to the file. it then goes to a newline.

any other suggestions?

would this have anything to do with components missing from my install or my IIS set up?

cheers,

mick
Oct 18 '07 #18
KevinADC
4,059 Expert 2GB
personally, I am suspecting that your form field name "email" is not what you really have in your form. In windows case is not significant, but in perl it is, if the form field name is "Email" (or whatever) you have to use that exact spelling in the perl script. Now if the form field name is correct, I am stumped, I don't know why the value of the form field is not printing to the file.
Oct 18 '07 #19
KevinADC
4,059 Expert 2GB
also, look for any error in the html code that might cause the form to break, such as a missing quote or a miss-spelled tag/attribute.
Oct 18 '07 #20
yep, my html file is good and I am running the same .pl file as posted. the file doesnt print any " ", just a newline each time.

well thanks for all your help everyone. i wont waste anymore of your time......im utterly confused as to y it wont work. time to go lick my wounds. :)

thanks

mick
Oct 18 '07 #21
eWish
971 Expert 512MB
Let's try to see if anything is coming from the form.
Expand|Select|Wrap|Line Numbers
  1. #!C:\perl\bin
  2. use strict;
  3. use warnings;
  4.  
  5. use CGI;
  6. use CGI::Carp qw/fatalsToBrowser/;
  7.  
  8. my $q = CGI->new;
  9. print $q->header('text/html'); 
  10. print $q->start_html(-title =>'Website Name'); 
  11.  
  12. my $email_file = 'c:/cgi-bin/email.txt';
  13. my $email_to_log = $q->param('email') || 'SomeEmail@email.com'; 
  14. my $email_in = $q->param('email') || 'Field was empty'; #If the incoming is empty then this will also show us.
  15.  
  16. print "Value of \$email_to_log:  $email_to_log<br>"; #This will tell if what email is being sent to the file.  Also shows if the $q->param('email') contains anything.
  17. print "Value of \$email_in:  $email_in<br>"; #This will print the value of the textfield 'email';
  18.  
  19.  
  20.     open my $EMAIL_LIST, '>>', $email_file || die "Can't open file $email_file: $!";
  21.     print $EMAIL_LIST $email_to_log, "\n";
  22.     close $EMAIL_LIST;
  23.  
  24. print "Go check your file and see what happened";
  25.  
  26.  
  27. print $q->end_html();
Try this and see what you get.
Oct 18 '07 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Michael Levin | last post by:
I've got the following problem. I'm a biologist and I have a device at work which monitors my frog habitat. The device has a bunch of sensors, and runs an embedded html server with some java...
6
by: rxl124 | last post by:
someone please please help w/ this one. As I been working on this on and off and it just does not want to work. 1 #!/usr/bin/perl -w 2 3 $file = "/home/user1/dothis"; 4 open(FILE, ">$file");...
8
by: von | last post by:
I am writing data from a Javascript to a text file using a Perl script and it all works pretty well - except ... This: "Here is my data" becomes: "Here%20is%20my%20data" when it gets to...
1
by: von | last post by:
I am trying to write a single piece of data (that is generated from a Javascript) to a text file on my server via a Perl script. The Javascript is setup so that I can display the required data on...
10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
6
by: rahulthathoo | last post by:
Hi I have my home directory on my departments server. Somehow I am not able to write to a file using a php code, $myFile = "trial3.txt"; $fh = fopen($myFile, "a+") or die("can't open file"); ...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
5
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for...
66
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.