Match regex and assign results in single line of code

my($variable) = "some string" =~ /(e\s*str)/;

This works because

If the /g option is not used, m// in list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, i.e., ($1, $2, $3 …).

and because my($variable) = ... (note the parentheses around the scalar) supplies list context to the match.

If the pattern fails to match, $variable gets the undefined value.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)