[Omaha.pm] simple syntax question

Hugh Jarce hjarce2001 at yahoo.com
Wed Jul 28 21:36:57 CDT 2004


Ryan Stille wrote:
> I need to alter the value of some elements passed in from a form, using
> a regular expression.   The only way I could think of to do it was like
> this:
> 
> $tmp1 = $FORM->param('respond_email');
> $tmp2 = $FORM->param('name');
> $tmp1 =~ s/\n|\r//g;
> $tmp2 =~ s/\n|\r//g;
> $FORM->param(-name=>'respond_email',-value=>$tmp1);
> $FORM->param(-name=>'name',         -value=>$tmp2);
> 
> Which I'm sure is not the most elegant.  Is there a way to do it
> without using the tmp variables?

I don't see how to avoid the tmps because s/// changes its operand
in place, while returning the number of substitutions made.
(BTW, you could avoid the temporary with a function like substr(),
because it returns the new value).

I suppose you could hide the temporary in a function like this:

sub remove_newlines {
    my $t = shift;
    $t =~ tr/\r\n//d;
    return $t;
}

allowing you to write code like this (untested):

$FORM->param(-name  => 'name',
             -value => remove_newlines( $FORM->param('name') ) );

BTW, tr/\r\n//d achieves the same result as s/\r|\n//g but is faster
because there's no need to compile no damn regex.

Hugh



	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 



More information about the Omaha-pm mailing list