2

I've inherited some perl scripts. (I'm not a perl programmer).

I'm seeing an error "can't find unicode property definition ascii" on the below line

$value =~ s/[^[:\p{ascii}]]//g 

Would this error cause the program execution to stop? As it's the last line printed before the program halts.

That same line has been run over 1,000 times before, before it gives up. What can the problem be?

I leaning towards that the value of $value is NOT what is causing the problem. Am I right?

It seems to me as though {ascii} has been removed from unicode definitions. Can this be done or am I completely barking up the wrong tree?

tchrist
  • 78,834
  • 30
  • 123
  • 180
Andi McLean
  • 51
  • 1
  • 5
  • I can't explain your code failing after running for a while, but the regex seems very odd. It matches "anything except '[', ':' or an ASCII Unicode character, followed by ']'. Since '[' and ':' are themselves ASCII Unicode characters, mentioning them at all is superfluous and it makes me think the regex is incorrect. Have you transcribed it incorrectly perhaps? Or if it is accurate, do you know what the pattern is supposed to match? – Borodin Jan 19 '12 at 13:31
  • And yes, it would cause your program to die unless the error is explicitly caught, which shouldn't be necessary here. – Borodin Jan 19 '12 at 13:34
  • Looks like a botched conversion from the [traditional POSIX characters class `[[:ascii:]]`](http://p3rl.org/recharclass#POSIX-Character-Classes) to the `\p`/`\P` Perl notation to me. – daxim Jan 19 '12 at 14:12
  • tripled checked the line, it is as written. $value is obtained from an snmp::get command. I'm guessing it's trying to remove all non ascii characters. – Andi McLean Jan 19 '12 at 14:53
  • We have other scripts which are nearly identical, that do not have that line in. I will just remove and see what happens. – Andi McLean Jan 20 '12 at 11:01

1 Answers1

2

It seems to me that ascii must be uppercase ASCII

$value =~ s/[^\p{ASCII}]//g 

test with \p{ascii}:

#> cat test.pl
#!/usr/bin/perl
my $str = q/☺ùùabvcedhkè ég"/;
$str =~ s/[^\p{ascii}]//g;
print $str,"\n";

#> perl test.pl
Can't find Unicode property definition "ascii" at test.pl line 3.

test with \p{ASCII}:

cat test.pl
#!/usr/bin/perl
my $str = q/☺ùùabvcedhkè ég"/;
$str =~ s/[^\p{ASCII}]//g;
print $str,"\n";

#> perl test.pl
abvcedhk g"
Toto
  • 89,455
  • 62
  • 89
  • 125
  • No. From `perldoc perluniprops`: "In parsing these constructs, Perl always ignores Upper/lower case differences everywhere within the {braces}" – Borodin Jan 19 '12 at 13:36
  • yes I did, and both of your cases work fine on my system (Perl v5.12.3). You may, though, have found the cause of the OP's problem. – Borodin Jan 19 '12 at 17:41
  • 2
    5.10 appears to be case-sensitive here, but 5.12 and 5.14 aren't. – ikegami Jan 19 '12 at 20:39
  • @ikegami: Right, as OP didn't say which version he's using, may be it's OK. – Toto Jan 20 '12 at 08:52
  • Just saying to which version he needs to upgrade if he wants to continue using lowercase :) (5.10 is no longer supported, fyi) – ikegami Jan 20 '12 at 09:41
  • we're using 5.10. Till I can get the time to test on the latest version, or rewrite the code. Strange thing is that code has already been called over 1,000 times before it throws a fit. – Andi McLean Jan 20 '12 at 10:59