G'day MikeTaylor,

Or perhaps something like this, if I could only find the right class name:

my $re = new Regexp($pattern); my $value = $re->substitute($value, replacement, $flags);

About 15 or 20 years ago, I read a book by Damian Conway called "Object Oriented Perl". In it, he shows the creation of blessed objects using various things including regular expressions: your post reminded me of this.

I don't own the book. If you can get a copy (you possibly already own one) it's certainly worth reading even though it's now quite old. If you follow the link I provided, you'll see a free PDF copy is offered; however, it looks like you need to "add to cart" which probably also means you have to "create an account" — I didn't follow through on this.

Here's a very quick-and-dirty implementation of a class which blesses regular expressions.

package Regex; use strict; use warnings; sub new { my ($class, $pattern, $flags) = @_; my $flag_part = defined $flags ? "(?$flags)" : ''; my $re_part = "\Q$pattern"; return bless qr{$flag_part$re_part}, $class; } sub match { my ($self, $str) = @_; return $str =~ $self ? 'YES' : 'NO'; } sub replace { my ($self, $str, $new) = @_; $str =~ s/$self/$new/; return $str; } 1;

If you want to use something like this in production code, it'll need a lot more work. What I've provided is only intended to demonstrate the basic principles involved. The book would probably provide a lot more information; but I don't remember details of something I read about two decades ago.

Here's a test of that module:

#!/usr/bin/env perl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; use Regex; my $pat = 'b'; my $case_sens_re_obj = Regex::->new($pat); my $case_insens_re_obj = Regex::->new($pat, 'i'); my $test_string = 'ABC'; print 'case_sens_re_obj match: ', $case_sens_re_obj->match($test_string), "\n"; print 'case_insens_re_obj match: ', $case_insens_re_obj->match($test_string), "\n"; print 'case_sens_re_obj substition: ', $case_sens_re_obj->replace($test_string, '_'), "\n"; print 'case_insens_re_obj substition: ', $case_insens_re_obj->replace($test_string, '_'), "\n";

Output:

case_sens_re_obj match: NO case_insens_re_obj match: YES case_sens_re_obj substition: ABC case_insens_re_obj substition: A_C

Unrelated but important: Please avoid indirect object syntax; e.g. new Regexp($pattern). See "perlobj: Indirect Object Syntax" for a discussion of problems with this syntax. The above example would be much better as Regexp::->new($pattern) — "perlobj: Invoking Class Methods" explains that.

P.S. When checking links prior to posting, I noticed "PDF for FREE" has been replaced with the text, "pBook + PDF". I don't know what that means and whether the PDF is still free or not (there would have only been a matter of minutes between finding the link in the first place and checking I had correctly included it in my post).

— Ken


In reply to Re: Regexp substitution using variables by kcott
in thread Regexp substitution using variables by MikeTaylor

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.