$HUGE_REGEX = " ... "; sub do_it { # The expression is recompiled whenever $HUGE_REGEX changes if ( $sexpert =~ m/$HUGE_REGEX/) { ... } } do_it(); # the expression is compiled do_it(); # the string didn't change so the compiled regex is re-used $HUGE_REGEX = " !!! "; do_it(); # the string changed so the regex is recompiled. do_it(); # the new regex re-used.