stevieb has asked for the wisdom of the Perl Monks concerning the following question:
I've got a very large and complex distribution where several of the modules use a pretty high number of somewhat complex regexes. I have decided instead of having them peppered throughout the code, I'd create a new module, Regex.pm that would house and return these regexes based on name.
Now, this all works well and fine after some fiddling and learning where certain flags need to be set. Here is a basic example:
use warnings; use strict; package Re; { my %h = ( re => qr/ [Pp]erl-\d\.\d+\.\d+(?:_\w+)? \s+===.*? (?=(?:[Pp]erl-\d\.\d+\.\d+(?:_\w+)?\s+===|$)) /xs, ); sub re { return $h{re}; } } package main; { my $str; { local $/; $str = <DATA>; } my $re = Re::re(); my @results = $str =~ /$re/g; print scalar @results; } __DATA__ perl-5.26.1 ========== Reading '/home/spek/.cpan/Metadata' Database was generated on Tue, 13 Feb 2018 15:29:02 GMT App::cpanminus is up to date (1.7043). --> Working on . Configuring /home/spek/repos/mock-sub ... OK <== Installed dependencies for .. Finishing. --> Working on . Configuring /home/spek/repos/mock-sub ... Generating a Unix-style Make +file Writing Makefile for Mock::Sub Writing MYMETA.yml and MYMETA.json OK Building and testing Mock-Sub-1.10 ... Skip blib/lib/Mock/Sub.pm (unch +anged) Skip blib/lib/Mock/Sub/Child.pm (unchanged) Manifying 2 pod documents PERL_DL_NONLAZY=1 "/home/spek/perl5/perlbrew/perls/perl-5.26.1/bin/per +l" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Har +ness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/00-load.t .................... ok t/01-called.t .................. ok t/02-called_count.t ............ ok t/03-instantiate.t ............. ok t/04-return_value.t ............ ok t/05-side_effect.t ............. ok t/06-reset.t ................... ok t/07-name.t .................... ok t/08-called_with.t ............. ok t/09-void_context.t ............ ok t/10-unmock.t .................. ok t/11-state.t ................... ok t/12-mocked_subs.t ............. ok t/13-mocked_objects.t .......... ok t/14-core_subs.t ............... ok t/15-remock.t .................. ok t/16-non_exist_warn.t .......... ok t/17-no_warnings.t ............. ok t/18-bug_25-retval_override.t .. ok t/19-return_params.t ........... ok t/manifest.t ................... skipped: Author tests not required fo +r installation t/pod-coverage.t ............... skipped: Author tests not required fo +r installation t/pod.t ........................ skipped: Author tests not required fo +r installation All tests successful. Files=23, Tests=243, 2 wallclock secs ( 0.13 usr 0.04 sys + 1.75 cu +sr 0.13 csys = 2.05 CPU) Result: PASS OK Successfully tested Mock-Sub-1.10
In the code, I've got this:
my $re = Re::re(); my @results = $str =~ /$re/g;
What I'm wondering, and haven't been able to sort out if it's possible, is skip the variable instantiation, and use the function call directly when using the regex, like this:
my @results = $str =~ /Re::re()/g;
Doable, or am I chasing down something impossible?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Use function as a regex
by choroba (Cardinal) on Feb 15, 2018 at 16:37 UTC | |
by salva (Canon) on Feb 15, 2018 at 16:59 UTC | |
|
Re: Use function as a regex
by salva (Canon) on Feb 15, 2018 at 17:06 UTC | |
by stevieb (Canon) on Feb 16, 2018 at 15:48 UTC | |
by Anonymous Monk on Feb 16, 2018 at 14:43 UTC | |
|
Re: Use function as a regex
by tybalt89 (Monsignor) on Feb 15, 2018 at 16:34 UTC | |
by stevieb (Canon) on Feb 15, 2018 at 16:36 UTC | |
by Eily (Monsignor) on Feb 15, 2018 at 17:01 UTC | |
|
Re: Use function as a regex
by Eily (Monsignor) on Feb 15, 2018 at 16:35 UTC | |
by stevieb (Canon) on Feb 15, 2018 at 16:39 UTC | |
by Anonymous Monk on Feb 15, 2018 at 16:50 UTC | |
by Eily (Monsignor) on Feb 15, 2018 at 17:17 UTC | |
by Anonymous Monk on Feb 15, 2018 at 18:29 UTC |