John
John

Reputation: 25

Perl and loopin through and storing Form Post input

Hi so what I'm ultimately trying to do here is get user input from a textarea in a form ( method POST) and basically read it line by line and store the lines in a kind of state variable or counter until a blank line is hit and the value of the state variable is pushed onto an array as one element. The state variable is then reset after this and the process continues until all the input is read. In visual terms, this is what it is supposed to do.

Example of user input:

some example user input to show

something with

hello world lorem ipsum

This is what I have so far. I had tried a foreach loop at first but it didn't work either

#!/usr/bin/perl

use strict;
use warnings;
use CGI;

my $CGI = new CGI();


sub main {

print $CGI->header();

my @query = $CGI->param('query');

while(@query) {
my $line = shift(@query);
print "$line";
}
#my $count = 0;
#my @res;
#foreach my $line(@query) {
#   if($line =~ /[A-Za-z0-9]/) {
#       push(@res, $line);
#   } elsif ($line =~ /^\s$/) {
#       $count++;
#   }
#}

print<<HTML;
<html>

<head>
    <style>
        textarea {
            resize: none;
        }
    </style>
</head>

<form action="rand.cgi" method="post">
<textarea name="query"></textarea>
<input type="submit"/>
</form>

<p>Last submitted:<br><br><pre>@query</pre></p>

</html>
HTML

}

main();

Upvotes: 1

Views: 76

Answers (1)

simbabque
simbabque

Reputation: 54333

There's no need for a bunch of regexes. You want to split on two consecutive line breaks. Try this:

use strictures;
use Data::Dump;

my $input = "some example user input to show
something with

hello world lorem ipsum";

my @foo = split /\n\n/, $input;
dd @foo;

__END__

(
  "some example user input to show\nsomething with",
  "hello world lorem ipsum",
)

Upvotes: 1

Related Questions