sub some_function { my $re = qr{ \A # start of string (?> x | y | z ) # match exactly one of x, y or z \z # end of string (ignore optional terminal newline) }x; while (<>) { if (/$re/} { # do something based on successful match } } }