Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone! I am trying to use CGI for the first time and I am wondering what it is I have to do to have the value of a large text box sent to a CGI script where it can be parsed and place each line in an array element. I would like it so that each line of the text box starts with a directory.
Example:
/vobs/multimedia
/vobs/testdirectory
this would write each line into a two element array. Right now I have my program reading from a text file into an array with
open (READ_BR, "br_sample.txt") or die ("Unable to open br_sample.txt!\n"); my @BR_paths = <READ_BR>; chomp @BR_paths; close (READ_BR);
But I am unsure of how to get the value of the form into the CGI script. I hope this makes sense. Thanks in advance!

Replies are listed 'Best First'.
Re: use CGI Question
by moritz (Cardinal) on Jul 24, 2008 at 15:35 UTC
    Use CGI and its param() method to retrieve the text. See also the Tutorials section on this site.
Re: use CGI Question
by olus (Curate) on Jul 24, 2008 at 16:00 UTC

    Sample code

    use CGI; my $query = new CGI; my $long_text_field = $query->param('long_text_field'); my @BR_paths = split /\n/, $long_text_field;
      Minor nit.

      A textarea comes in with carriage return/line feed pairs.

      /\n/ will leave the carriage returns behind (on windows and nix). It may be better to use /\r\n/ instead.

      #!c:/perl/bin/perl.exe use strict; use warnings; use CGI; my $q = CGI->new; #my @lines = split /\n/, $q->param(q{txt_area}); my @lines = split /\r\n/, $q->param(q{txt_area}); print $q->header; print qq{*$_*} for @lines;
      <html> <head> <title>textarea test</title> </head> <body> <form name = "txt_area_form" action = "/z/bin/test_form.cgi"> <textarea name = "txt_area" rows = "3" cols = "10"></textarea> <input type = "submit"> </form> </body> </html>
      /\r\n/ produces
      *one**two**three*
      and /\n/ gives
      *one **two **three*
      i.e. the carriage returns are still lurking waiting to bite you later on. :-)
A reply falls below the community's threshold of quality. You may see it by logging in.