Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Force ASCII regexp for all perls 5.8+

by tobyink (Abbot)
on May 16, 2013 at 09:17 UTC ( #1033796=note: print w/replies, xml ) Need Help??

in reply to Force ASCII regexp for all perls 5.8+

# First we'll define this pragma called Regexp::Ascii. This is where # the magic happens... # BEGIN { package Regexp::Ascii; $INC{'Regexp/'} = __FILE__; use overload (); use Carp qw(croak carp); our %replace = ( '\d' => '[0-9]', '\D' => '[^0-9]', '\w' => '[A-Za-z0-9_]', '\W' => '[^A-Za-z0-9_]', ); sub import { shift; carp "Regexp::Ascii used with no parameters" unless @_; for (@_) { croak "Regexp::Ascii does not know what to do with '$_'" unless exists $replace{$_}; } my $find = join '|', map quotemeta, sort { length $b <=> length $a or $a cmp $b } @_; overload::constant 'qr' => sub { my $find = (my $re = shift) =~ s/($find)/$replace{$1}/eg; return $re; } } sub unimport { overload::remove_constant 'qr'; } }; # Here's our test case... # use strict; use warnings; use Test::More tests => 3; use constant ARABIC_INDIC_0 => "\x{0660}"; like(ARABIC_INDIC_0, qr/^\d$/, 'unicode'); { # use our pragma in this scope use Regexp::Ascii qw( \d \D ); unlike(ARABIC_INDIC_0, qr/^\d$/, 'ascii'); } like(ARABIC_INDIC_0, qr/^\d$/, 'unicode');
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Force ASCII regexp for all perls 5.8+
by vsespb (Chaplain) on May 16, 2013 at 10:32 UTC
    Thanks! Also I am thinking now - one needs not to forget to implement also "\D" (non-digit) "\W" (non-word) etc

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1033796]
[Corion]: choroba: Naah, I don't mind rewriting history, at least when it is in my favour ;)
[Corion]: Ruining other peoples clones is not nice, but as I only have that repo to (later) show people when I encounter problems, I don't mind that
[Corion]: I should look into self-hosting gitprep instead of relying so much on github

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-01-23 10:35 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (192 votes). Check out past polls.