On Thu, 12 Jul 2012 21:01:35 +0200
Manfred Lotz <manfred.l...@arcor.de> wrote:

...
> > 
> 
> On the one hand I believe there must be a 'better' way. On the other
> hand I like the idea (didn't occur to me) to read from the string as
> if it were a file.
> 

I found something in a blog from Jan Ploski:
http://plosquare.blogspot.de/2009/04/viewing-internal-representation-of.html


# For a given string parameter, returns a string which shows
# whether the utf8 flag is enabled and a byte-by-byte view
# of the internal representation.
#
sub hexdump
{
    my $str = shift;
    my $flag = Encode::is_utf8($str) ? 1 : 0;
    use bytes; # this tells unpack to deal with raw bytes
    my @internal_rep_bytes = unpack('C*', $str);
    return
        $flag
        . '('
        . join(' ', map { sprintf("%02x", $_) } @internal_rep_bytes)
        . ')';
}


This is really nice. I fumbled with unpack before but have to admit
that I didn't know about 'use bytes' which is the key.



-- 
Manfred



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to