in reply to Putting the stringified Regexp object into code

Do you really need to stringize the entire if-statement? If you can get away with it, it's much simpler to use the stringized regex object on its own. (When a match such as  m// or  s/// is compiled, the compiler looks for terminating delimiters before interpolating scalars, so embedded '/' characters in the scalars should not be a problem.) (Also: The  =~ operator will bind to a plain old string.)

>perl -wMstrict -le "my $rx = qr{ foo/bar }xms; print 'ref: ', ref $rx; $rx = qq{$rx}; print 'de-refized: ', ref $rx; print $rx; ;; my $str = 'xx foo/bar yy'; print 'match' if $str =~ $rx; print qq{captured '$1'} if $str =~ m/($rx)/; " ref: Regexp de-refized: (?^msx: foo/bar ) match captured 'foo/bar'

Replies are listed 'Best First'.
Re^2: Putting the stringified Regexp object into code
by sedusedan (Monk) on Nov 06, 2012 at 19:11 UTC

    Yes, basically I want to generate a Perl code (as string) where an input Regexp object needs to be a literal in the generated code.

    BTW, the project is Data::Sah and here's an example of the code generation in action:

    $ TRACE=1 LOG_SAH_VALIDATOR_CODE=1 perl \ -MLog::Any::App -MData::Sah=gen_validator \ -E'$v = gen_validator([str => {match=>qr!/!}]); for ("a", "a/") { say "$_: ", $v->($_) }' ... [98] validator code: 1|sub { 2| my ($data) = @_; 3| my $res = 4| # skip if undef 5| (!defined($data) ? 1 : 6| (# check type 'str' 7| (!ref($data)) 8| && 9| (# clause: match 10| ($data =~ qr/(?^u:\/)/)))); 11| return $res; 12|} a: a/: 1


      The reason kennethk is wondering why you're avoiding closures is that much template-generated code can be easily replaced with a closure. Generally a string eval can be replaced with something better.

      Munging strings to generate functions is certainly possible, and many have done it. But sometimes it's just easier to generate the function itself. An example (very loosely) based on your sample code:

      $ cat #!/usr/bin/perl use strict; use warnings; # Function to accept only 4 digit numbers my $v_4dig = gen_validator('^\d{4}$'); # Function to accept only 3 digit numbers my $v_3dig = gen_validator('^\d{3}$'); # Function to accept only lower-case alphabetic strings my $v_alpha = gen_validator('^[a-z]+$'); for my $t ('apple', 123, 456, 7890) { print "$t:\t", $v_4dig->($t), "\t", $v_3dig->($t), "\t", $v_alpha- +>($t), "\n"; } sub gen_validator { my $regex = shift; # Create function to validate against current regex return sub { my $data = shift; return 1 if ! defined $data; if (ref $data eq '') { return 1 if $data =~ /$regex/; } return 0; } } $ perl apple: 0 0 1 123: 0 1 0 456: 0 1 0 7890: 1 0 0

      Update: Added the third validator function example, just for variety. Also added a couple of comments.

      Update: Re-read thread and properly attributed suggestion.


      When your only tool is a hammer, all problems look like your thumb.

        Thanks for the suggestion. However, in this case, I really do want to generate code string, because I want to save the code to disk for embedding it in other code, among other uses. For example in Dist::Zilla::Plugin::Rinci::Validate.