Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Character Class in Perl 5.018

I have a parsing environment (Marpa::R2::Scanless) that needs to use single Perl regexp character classes to control tokenizing. I've got something to tokenize that doesn't seem to fit any of the existing character classes. So, after digging around in the perlunicode docs, I've come up with the following code, except it doesn't work as expected. I expect to see a row of dots interspersed with all the non-alphanumerics (except parens). Instead, I get a runtime error about not being able to find the character class.

#!/usr/bin/env perl

use 5.018;
use utf8;

local $| = 1;

for my $i (map { chr($_) } 32 .. 127) {
    if ($i =~ /\p{Magic::Wow}/) {
        print $i;
    }
    else {
        print ".";
    }
}

package Magic;

sub Wow {
    return <<'MAGIC';
+utf8::Assigned
-utf8::Letter
-utf8::Number
-0028
-0029
MAGIC
}

1;

Any hints, tips, tricks, or suggestions?

like image 536
PWBENNETT Avatar asked Jul 16 '13 10:07

PWBENNETT


1 Answers

Name the sub IsWow and the property Magic::IsWow.

Quoting User-Defined Character Properties in perlunicode:

You can define your own binary character properties by defining subroutines whose names begin with "In" or "Is".

like image 104
daxim Avatar answered Sep 26 '22 18:09

daxim