use CGI qw(:standard); my $text_field = param('text_field') || ''; $text_field =~ /\A( \w{0,50} # Only let through 50 word charcters, at most )/x; $text_field = $1;