Since we went to a lot of trouble to get lexical and closures to work correctly 
in Perl 6, it seems fair to use it here:

    $ cat rxstr
    sub rxstr($s) { rx/<$s>/ }
    
    my $str = 'foo';
    my $foorx = rxstr($str);   # create /foo/ regex
    
    say 'foo' ~~ $foorx;       # matches
    $str = 'bar'; 
    say 'foo' ~~ $foorx;       # still matches... $foorx is unchanged
    
    $ ./perl6 rxstr
    「foo」
    「foo」

And for a bit more golfing, there's:

    my $str = 'foo';
    my $foorx = { rx/<$^a>/ }.($str);
    
    say 'foo' ~~ $foorx;

Pm


On Fri, May 12, 2017 at 01:01:17AM +0200, Timo Paulssen wrote:
> The only way that comes to mind is to use EVAL, but that's not
> golf-friendly at all.
> 
> Perhaps you can find something sufficiently short based on .contains,
> .index, .starts-with, .ends-with, and friedns?

Reply via email to