in reply to qr and substitution

Just a little comment that you can use embedded pattern match modifiers in your regular expression.

$pattern = "foobar"; if ( /$pattern/i ) { } # equivalent to the more flexible form of $pattern = "(?i)foobar"; if ( /$pattern/ ) { }