in reply to How do I safely, portably extract one or more bytes from a string?

Check out the bytes pragma.
Update:

With the previous solution you can then use substr normally, but it would return by byte instead of by character.

Or, perhaps more clearly, you could use the vec function with a third argument of 8. This has the advantage of being an lvalue (the bytes::substr() function isn't, unfortunately).



Who is Kayser Söze?
  • Comment on Re: How do I safely, portably extract one or more bytes from a string?
  • Download Code

Replies are listed 'Best First'.
Re: Re: How do I safely, portably extract one or more bytes from a string?
by pg (Canon) on Nov 29, 2003 at 03:24 UTC

    Just to demo "use byte" as jweed mentioned.

    use strict; my $a = chr(4000); { use bytes; print substr($a, 0, 1), "\n"; } { print substr($a, 0, 1), "\n"; }

    You will see a warning saying "wide character in print", don't worry about that, that is what supposed to happen.