Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Use function as a regex

by Eily (Monsignor)
on Feb 15, 2018 at 16:35 UTC ( [id://1209234]=note: print w/replies, xml ) Need Help??


in reply to Use function as a regex

How about just Re::re($str) instead? Or maybe $str =~ /$Re::h{re}/g

Without the /g you would have been able to use $str =~ Re::re(). But to have the global search I can only think of $str =~ /@{[ Re::re() ]}/g or /(??{ Re::re() })/g which aren't much of an improvement.

Can your subs have parameters? If there is one regex for each sub, where the name of the sub is also the key in the hash, you maybe be interested in AUTOLOAD.

Edit: choroba made me realize it's (??{ }) not (?{{ }}) (which should have been obvious, as the latter is actually a case of (?{ }))

Replies are listed 'Best First'.
Re^2: Use function as a regex
by stevieb (Canon) on Feb 15, 2018 at 16:39 UTC

    The sub(s) will take parameters. One sub per module in the distribution. Here's the real code section of the new module. I've only added one module's sub so far:

    package Test::BrewBuild::Regex; use strict; use warnings; use Carp qw(croak); use Exporter qw(import); our $VERSION = '2.20'; our @EXPORT = qw( re_brewbuild ); my %brewbuild = ( check_failed => qr{ failed.*?See\s+(.*?)\s+for details }x, check_result => qr{ [Pp]erl-\d\.\d+\.\d+(?:_\w+)? \s+===.*? (?=(?:[Pp]erl-\d\.\d+\.\d+(?:_\w+)?\s+===|$)) }xs, extract_dist_name => qr{ ^name\s+=\s+(.*)$ }x, extract_dist_version => qr{ ^version\s+=\s+(.*)$ }x, extract_errors => qr{ cpanm\s+\(App::cpanminus\) .*? (?=(?:cpanm\s+\(App::cpanminus\)|$)) }xs, extract_error_perl_ver => qr{ cpanm.*?perl\s(5\.\d+)\s }x, extract_result => qr{ ([Pp]erl-\d\.\d+\.\d+(?:_\w+)?\s+=+?) (\s+.*?) (?=(?:[Pp]erl-\d\.\d+\.\d+(?:_\w+)?\s+===|$)) }xs, extract_perl_version => qr{ ^([Pp]erl-\d\.\d+\.\d+(_\d{2})?) }x, ); sub re_brewbuild { my $re = shift; _check(\%brewbuild, $re); return $brewbuild{$re}; } sub _check { my ($module, $re) = @_; croak "regex '$re' doesn't exist for re_brewbuild()" if ! exists $module->{$re}; } 1;
Re^2: Use function as a regex
by Anonymous Monk on Feb 15, 2018 at 16:50 UTC

    You can probably use straight-up $str =~ re() syntax with global searches as well, just make sure the re() returns a :lvalue. Either that or return a ref: $str =~ ${re()}.

      I don't see any connexion between :lvalue and /g. $str =~ ${re()} isn't a global search but the output of qr is already a reference so /${re()}/g works, as pointed out by salva.

        Embarrasing. I read it exactly backwards. Something like while (str() =~ /(.)/g) can be made to work by using sub str :lvalue { state $x = "abcd" }.

        Sorry about the noise.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209234]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-24 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found