in reply to Check user input - reimplementation

$checks{'data'} =~ s/\s+/ /g; # remove leading and trailing blanks $checks{'data'} =~ s/^\s+//; $checks{'data'} =~ s/\s+$//;
I'd do this in reverse order, so that you aren't substituting in a space that you're later going to remove. That's just a tiny amount of extra work. If the spaces are really spaces and not tabs, you can squeeze them using tr/ //s instead of s/\s+/ /g.
$checks{'regex'} = $checks{'regex'} ? $checks{'regex'} : $default;
The common idiom for this is
$checks{'regex'} ||= $default;
but be sure you mean that all false values should be replaced with default. You may have meant, instead
exists($checks{'regex'}) or $checks{'regex'} = $default;
I note that regex is really expected to be a character class. It would be simple to take a real regex (qr// style) instead.

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
Re: Re: Check user input - reimplementation
by kiat (Vicar) on Dec 15, 2003 at 23:25 UTC
    Thanks, Roy!

    I'd do this in reverse order, so that you aren't substituting in a space that you're later going to remove. That's just a tiny amount of extra work.
    You mean like this?
    $checks{'data'} =~ s/\s+$//; $checks{'data'} =~ s/^\s+//; $checks{'data'} =~ s/\s+/ /g;
    Okay so
    $checks{'regex'} ||= $default;
    is the same as
    $checks{'regex'} = $checks{'regex'} ? $checks{'regex'} : $default;
    But what does the following do?
    exists($checks{'regex'}) or $checks{'regex'} = $default;
      You mean like this?
      Yes.

      A ||= D is, in general, the same as A = A ?  A : D without all the repetition. Each assigns to A itself if A is true, and D if it is not true.

      The exists check is the same as

      $check{'regex'} = $default unless exists($checks{'regex'})
      which is to say, it determines whether there a value has been assigned to $checks{'regex'}, and if not, it assigns the default value. Even if the value is false or undefined, exists returns true.

      The PerlMonk tr/// Advocate