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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chez
    New Member
    • May 2011
    • 9

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

    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!
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    What's a text box?

    - Miller

    Comment

    • chez
      New Member
      • May 2011
      • 9

      #3
      I will be using cgi.
      Will this work?

      Code:
      <form action="your_file.cgi" method="GET">
      ....
      <textarea name="texttosave" cols=30 rows=40></textarea><br>
      ...
      <input type="submit">
      </form>
      you can save the text using the following script:
      Code:
      #!usr/bin/perl
      use CGI;
      my $query=new CGI;
      my $txt=$query->param("texttosave");
      open(SAVEFILE,"your_file_name") || die"Cannot open file.\n";
      print SAVEFILE $txt || die "Cannot print data.\n";
      close (SAVEFILE) || die"Cannot save data.";
      exit(0);
      Last edited by numberwhun; May 18 '11, 01:42 PM. Reason: Please use code tags around your code! They are required in the forums.

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        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.
        Code:
        use strict;
        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.

        Code:
        my $file = 'your_file_name';
        open my $output_fh, '>', $file or die"Cannot open <$file>. $!\n";

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          Putting the good suggestions from RonB into practice, gives you the following script:

          Code:
          #!/usr/bin/perl -wT
          
          use CGI;
          use CGI::Carp qw(fatalsToBrowser);
          
          use strict;
          use warnings;
          
          my $file = 'your_file_name';
          
          my $q = new CGI;
          
          if ($q->request_method() eq 'POST') {
          	my $txt = $q->param("texttosave");
          	open my $fh, '>', $file or die "can't open $file: $!";
          	print $fh $txt;
          	close $fh;
          	print $q->header();
          	print <<"END_HTML";
          <html>
          <head><title>Success</title></head>
          <body><p>File successfully saved</p></body>
          </html>
          END_HTML
          	exit;
          }
          
          print $q->header();
          print <<"END_HTML";
          <html>
          <head><title>Save Textarea</title></head>
          <body>
          <form method="POST" action="your_file.cgi">
          <p>Enter text to save:</p>
          <p><textarea name="texttosave" cols=30 rows=40></textarea></p>
          <p><input type="submit"></p>
          </form>
          </body>
          </html>
          END_HTML

          Comment

          Working...