jroberts has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i'm parsing some files that have embedded binary data at various random locations. is there a function or module that implements something like the old libc function isachar()? I'd like to be able to detect these binary characters. Thanks in advance.

Replies are listed 'Best First'.
Re: isachar() equivalent?
by Zaxo (Archbishop) on Jul 03, 2001 at 07:21 UTC

    You can define a character class for the range you want to detect:

    my $bins=qr/[\x80-\xff]/; # or whatever m/$bins/ && my_action();

    After Compline,
    Zaxo

Re: isachar() equivalent?
by tadman (Prior) on Jul 03, 2001 at 08:32 UTC
    I'm not sure exactly what you mean by isachar, since that doesn't appear to be one of the ANSI C standard functions. However, taking Zaxo's code one step further:
    sub isachar { return pop =~ /^[\x00-\x7F]/; } # Or alternatively sub isalnum { return pop =~ /^\w/; } # As in: $is = isachar('a');
    Update:
    crazyinsomiac pointed out "pop is not a variable", but it is a quick way of retrieving the first element of @_, a.k.a. $_[0].
Re: isachar() equivalent?
by clintp (Curate) on Jul 03, 2001 at 17:58 UTC
    You might also want to consider using one of the POSIX character classes. They're a little more flexible than the \w or \d support normally found in regexps.

    perldoc perlre and search for POSIX for more info.