kennethk is right; store a stringified pattern constructed with the qr// operator. But there's a distinction to be made between storing a reference to a Regexp object, and storing a stringified Regexp object. Consider the following code:
my $regexp = qr/PATTERN/;
print {$FH} $regexp; # writes (?^u:PATTERN) to file.
The Regexp object returned by the qr// operator has stringification overloaded so that you never see something like Regexp(0xabcdef). This is very convenient, as it means that Regexp objects are easily interpolated into larger patterns, larger Regexp objects, or into strings as larger patterns are assembled.
But there is one nuance here that might be missed: A Regexp object is a compiled regular expression. Stringifying a regexp object returns a string representation of a version of the original pattern that is semantically identical (if not syntactically) to the original pattern. But it is only a string, and before it's useful to Perl on an internals level, it will need to be compiled again. This happens quietly behind the scenes. But it does carry with it a (minor) runtime cost.
Nevertheless, it is the best approach. Just be aware that you're storing a string returned by implicit stringification of a Regexp object; you're not storing the object itself, nor a reference to it.
|