Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2018-01-17 06:02 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (196 votes). Check out past polls.