String::Dump - Dump strings of characters or bytes for printing and debugging
This document describes String::Dump version 0.01.
use String::Dump; say 'hex: ', dump_string($string); # hex mode by default say 'oct: ', dump_string(oct => $string); # octal mode
This is an early release of String::Dump. Feedback is appreciated! To give suggestions or report an issue, contact mailto:patch@cpan.org or open an issue at https://github.com/patch/string-dump-pm5/issues. Pull requests are welcome at https://github.com/patch/string-dump-pm5.
This module provides the function dump_string and exports it by default. When debugging or reviewing non-ASCII data, dump_string is your friend. It's a simple utility to view the characters or bytes of your string in several different formats, such as hex, octal, decimal, Unicode names, and more.
dump_string
An OO interface is forthcoming with additional options and the ability to reuse them among multiple calls. Some benefits will be the ability to set the delimiter between characters and to force a string to be treated as a string of characters or a series of bytes. Don't worry, the dump_string function will remain simple.
The mode is optional and defaults to hex. Other valid modes are dec, oct, bin, and names, and are described below. The string may either be a series of Unicode characters or binary bytes.
hex
dec
oct
bin
names
Hexadecimal (base 16) mode. This is the default when only a string is passed without the mode.
use utf8; # string of 6 characters say dump_string('Ĝis! ☺'); # 11C 69 73 21 20 263A say dump_string(hex => 'Ĝis! ☺'); # same thing no utf8; # series of 9 bytes say dump_string('Ĝis! ☺'); # C4 9C 69 73 21 20 E2 98 BA
For a lowercase hex dump, simply pass the response to lc.
lc
say lc dump_string('Ĝis! ☺'); # 11c 69 73 21 20 263a
Decimal (base 10) mode.
use utf8; say dump_string(dec => 'Ĝis! ☺'); # 284 105 115 33 32 9786 no utf8; say dump_string(dec => 'Ĝis! ☺'); # 196 156 105 115 33 32 226 152 186
Octal (base 8) mode.
use utf8; say dump_string(oct => 'Ĝis! ☺'); # 434 151 163 41 40 23072 no utf8; say dump_string(oct => 'Ĝis! ☺'); # 304 234 151 163 41 40 342 230 272
Binary (base 2) mode.
use utf8; say dump_string(bin => 'Ĝis! ☺'); # 100011100 1101001 1110011 100001 100000 10011000111010 no utf8; say dump_string(bin => 'Ĝis! ☺'); # 11000100 10011100 1101001 1110011 100001 100000 11100010 10011000 10111010
Named Unicode character mode. Unlike the various numeral modes above, this mode uses ', ' for the delimiter.
use utf8; say dump_string(names => 'Ĝis! ☺'); # LATIN CAPITAL LETTER G WITH CIRCUMFLEX, LATIN SMALL LETTER I, # LATIN SMALL LETTER S, EXCLAMATION MARK, SPACE, WHITE SMILING FACE
This mode makes no sense for a series of bytes, but it still works if that's what you really want!
no utf8; say dump_string(names => 'Ĝis! ☺'); # LATIN CAPITAL LETTER A WITH DIAERESIS, STRING TERMINATOR, # LATIN SMALL LETTER I, LATIN SMALL LETTER S, EXCLAMATION MARK, # SPACE, LATIN SMALL LETTER A WITH CIRCUMFLEX, START OF STRING, # MASCULINE ORDINAL INDICATOR
The output in the examples above has been manually split into multiple lines for the layout of this document.
When dumping literal strings in your code, as in the examples above, use the utf8 pragma when strings of Unicode characters are desired and don't use it or disable it when series of bytes are desired. The pragma may also be lexically enabled or disabled.
use utf8; { no utf8; say dump_string('Ĝis! ☺'); # C4 9C 69 73 21 20 E2 98 BA } say dump_string('Ĝis! ☺'); # 11C 69 73 21 20 263A
The simplest way to ensure that you're working with strings of characters from all of your basic sources of input is to use the utf8::all pragma. This extends the utf8 pragma to automatically convert command-line arguments provided by @ARGV, user-defined filehandles, as well as STDIN, among others.
@ARGV
STDIN
To handle strings provided by other sources of input, such as from network protocols or a web server request, pass the value to Encode::decode_utf8, which will return the desired string.
use Encode; say dump_string( decode_utf8($string) );
To convert a variable in-place, pass it to utf8::decode instead.
utf8::decode($string); say dump_string($string);
Data::HexDump - Simple hex dumping using the default output of the Unix hexdump utility
hexdump
Data::Hexdumper - Advanced formatting of binary data, similar to hexdump
Nick Patch <patch@cpan.org>
Copyright 2011 Nick Patch
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install String::Dump, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::Dump
CPAN shell
perl -MCPAN -e shell install String::Dump
For more information on module installation, please visit the detailed CPAN module installation guide.