473,396 Members | 1,689 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,396 software developers and data experts.

how to save text from textBox to a text file using Perl in Unix platform

9
Hi,

I want to save the text from a text box to a text file, but i have problems implementing it.
can someone guide me?

Thanks!
May 9 '11 #1
4 4962
miller
1,089 Expert 1GB
What's a text box?

- Miller
May 9 '11 #2
chez
9
I will be using cgi.
Will this work?

Expand|Select|Wrap|Line Numbers
  1. <form action="your_file.cgi" method="GET">
  2. ....
  3. <textarea name="texttosave" cols=30 rows=40></textarea><br>
  4. ...
  5. <input type="submit">
  6. </form>
you can save the text using the following script:
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. use CGI;
  3. my $query=new CGI;
  4. my $txt=$query->param("texttosave");
  5. open(SAVEFILE,"your_file_name") || die"Cannot open file.\n";
  6. print SAVEFILE $txt || die "Cannot print data.\n";
  7. close (SAVEFILE) || die"Cannot save data.";
  8. exit(0);
May 10 '11 #3
RonB
589 Expert Mod 512MB
You opened your filehandle in read mode which is not what you want and is only one of a number of mistakes made in that script.

Every Perl script you write should include. these 2 pragmas.
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
You should use lexical vars for filehandles and the 3 arg form of open and the die statement should include the filename and $! which is the reason it failed.

Expand|Select|Wrap|Line Numbers
  1. my $file = 'your_file_name';
  2. open my $output_fh, '>', $file or die"Cannot open <$file>. $!\n";
May 10 '11 #4
miller
1,089 Expert 1GB
Putting the good suggestions from RonB into practice, gives you the following script:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -wT
  2.  
  3. use CGI;
  4. use CGI::Carp qw(fatalsToBrowser);
  5.  
  6. use strict;
  7. use warnings;
  8.  
  9. my $file = 'your_file_name';
  10.  
  11. my $q = new CGI;
  12.  
  13. if ($q->request_method() eq 'POST') {
  14.     my $txt = $q->param("texttosave");
  15.     open my $fh, '>', $file or die "can't open $file: $!";
  16.     print $fh $txt;
  17.     close $fh;
  18.     print $q->header();
  19.     print <<"END_HTML";
  20. <html>
  21. <head><title>Success</title></head>
  22. <body><p>File successfully saved</p></body>
  23. </html>
  24. END_HTML
  25.     exit;
  26. }
  27.  
  28. print $q->header();
  29. print <<"END_HTML";
  30. <html>
  31. <head><title>Save Textarea</title></head>
  32. <body>
  33. <form method="POST" action="your_file.cgi">
  34. <p>Enter text to save:</p>
  35. <p><textarea name="texttosave" cols=30 rows=40></textarea></p>
  36. <p><input type="submit"></p>
  37. </form>
  38. </body>
  39. </html>
  40. END_HTML
  41.  
May 13 '11 #5

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

Similar topics

0
by: harshan | last post by:
Hi I am trying to read required worksheet from excel file using perl with module spreadsheet::parseexcel and spreadsheet::writeexcel but I am unable to read it, the error I am getting is...
2
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
1
by: Kriston Scott | last post by:
I would like to know how to sort the rows in an Excel file, using Perl.
2
by: prakharv | last post by:
Hi All, I would like to upload a image file from my web page to the server using perl scripting. Please, if any one knows regarding this or has the source code for the same, please help me...
3
by: sainiamit25 | last post by:
Hi, I am trying to set up environment for XML parsing using perl. I downloaded XML::Simple and installed it too. When i try to run my program, it says, you need to install parser. When i try to...
4
by: chandru1985 | last post by:
Hi Guys , Can u help me out in Pel Coding as i'm new to perl . Can any one of them say whether it to print a line in a colored way in the output file using Filehandler .. Codin is :::...
1
by: prashantkuppa | last post by:
please help me anyone, here i am struck up with extracting check box status from pdf file using perl please give anybody some suggestions its urgent thanks prashant
1
by: sbettadpur | last post by:
hello, I have a bulk of text in text file. so i want to send the text to database using perl is it possible? or do we have to convert text file to any other format if it is yes please let me know ...
3
crazy4perl
by: crazy4perl | last post by:
Hi All, I have some doubt related to xml. Actually I want to update a file which is in some format. So I am converting that file using Tap3edit perl module in a hash. Now I m trying to create a...
1
by: msxi | last post by:
Hi, I have s sed script that removes the following lines from a db dump file. I would like to remove these lines using perl. Could anybody assist? /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.