in reply to testing if a string is ascii

No, that isn't enough. Go the other direction, negating the list of things that are what you want:

$str !~ /[^\0-\x7f]/

- tye        

Replies are listed 'Best First'.
Re^2: testing if a string is ascii (^)
by Neutron Jack (Pilgrim) on Sep 04, 2006 at 06:22 UTC
    Or, $str !~ /[^[:ascii:]]/
Re^2: testing if a string is ascii (^)
by graff (Chancellor) on Sep 04, 2006 at 15:41 UTC
    Actually, I think the better way would be to return true on the first non-ASCII character, and treat "true" as the condition to be avoided:
    sub nonascii { local $_ = shift; /[^\0-\x7f]/; # or /[^[:ascii:]]/ } for my $test ( split( " ", "foo bar b\xe1 baz" )) { next if ( nonascii( $test )); print "$test\n"; }
    (update: this is just a stylistic difference relative to  $str !~ /[^\0-\x7f]/ -- to avoid the cognitive challenge of the double-negative)