use strict; use warnings; my @constants = qw(foo bar baz); my $match = '\b' . join ('\b|\b', @constants) . '\b'; while () { print if /$match/; } __DATA__ A foo line no hit bar line barren line No food here foo_bar_baz doesn't hit too #### A foo line bar line