473,799 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot write from perl CGI to text file

9 New Member
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
21 5823
eWish
971 Recognized Expert Contributor
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
Mick1000
9 New Member
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 Recognized Expert Contributor
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
Mick1000
9 New Member
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 Recognized Expert Contributor
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
Mick1000
9 New Member
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....picki ng 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 Recognized Expert Specialist
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
Mick1000
9 New Member
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 Recognized Expert Specialist
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 Recognized Expert Specialist
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

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

Similar topics

14
3060
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 functions defined which know how to read the hardware sensorts. I access it from wherever I am via any browser, and it displays the measurements (a set of simple numbers) as Java applets in a simple html page. One entry in this page (a table cell) looks...
6
3844
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"); 5 while($line = <FILE>) { 6 if ($line =~ /B/) {print FILE "A"}; 7 if ($line =~ /A/) {print FILE "B"};
8
6258
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 the text file.
1
1735
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 my website using the following HTML: <span name='Data1' class='data1'></span> But I need it sent to a text file. :(
10
4475
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 Enterplise Linux 3 ES, and applied FixPack fp5_mi00069.tar to it. After creating an instance, starting the database, creating a database, and entering the table definitions, all of which seems to work OK, I entered a tiny 8-row table and can do...
6
3361
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"); Instead of a+ if i use r or w or anything, it does not work. Could this be a permission thing? The entire directory had a 777 permission and even i after i created a file and chmod'ed 777 to it, the code couldnt open it. What could be the matter?...
21
34445
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
5
2328
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 explaining a "lamda" expression and so I don't know what one is and I am not doing any database stuff in the entire program. So what does this error message really mean and what can I do to get an On Error into the routine?
66
8203
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 didnt display the html after the perl script executed. Using perl 5.1 and apache 2.2.9 version(apache installed and run without any errors and no warning, perl tested fine) b) Also, when i clicked the html code to submit the upload of the...
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.