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.


Dave


In reply to Re: Storing regexen in a DB and using them by davido
in thread Storing regexen in a DB and using them by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.