NAME
Data::Dumper::Interp - Data::Dumper for humans, with interpolation
SYNOPSIS
use open IO => ':locale';
use Data::Dumper::Interp;
@ARGV = ('-i', '/file/path');
my %hash = (abc => [1,2,3,4,5], def => undef);
my $ref = \%hash;
# Interpolate variables in strings with Data::Dumper output
say ivis 'FYI ref is $ref\nThat hash is: %hash\nArgs are @ARGV';
# -->FYI ref is {abc => [1,2,3,4,5], def => undef}
# That hash is: (abc => [1,2,3,4,5], def => undef)
# Args are ("-i","/file/path")
# Label interpolated values with "expr="
say dvis '$ref\nand @ARGV';
# -->ref={abc => [1,2,3,4,5], def => undef}
# and @ARGV=("-i","/file/path")
# Functions to format one thing
say vis $ref; #-->{abc => [1,2,3,4,5], def => undef}
say vis \@ARGV; #-->["-i", "/file/path"] # any scalar
say avis @ARGV; #-->("-i", "/file/path")
say hvis %hash; #-->(abc => [1,2,3,4,5], def => undef)
# Stringify objects
{ use bigint;
my $struct = { debt => 999_999_999_999_999_999.02 };
say vis $struct;
# --> {debt => (Math::BigFloat)999999999999999999.02}
}
# Wide characters are readable
use utf8;
my $h = {msg => "My language is not ASCII ☻ ☺ 😊 \N{U+2757}!"};
say dvis '$h' ;
# --> h={msg => "My language is not ASCII ☻ ☺ 😊 ❗"}
#-------- OO API --------
say Data::Dumper::Interp->new()
->MaxStringwidth(50)->Maxdepth($levels)->vis($datum);
say visnew->MaxStringwidth(50)->Maxdepth($levels)->vis($datum);
#-------- UTILITY FUNCTIONS --------
say u($might_be_undef); # $_[0] // "undef"
say quotekey($string); # quote hash key if not a valid bareword
say qsh($string); # quote if needed for /bin/sh
say qshpath($pathname); # shell quote excepting ~ or ~username prefix
system "ls -ld ".join(" ",map{ qshpath }
("/tmp", "~sally/My Documents", "~"));
DESCRIPTION
This is a wrapper for Data::Dumper optimized for consumption by humans instead of machines; the output defaults to higher-level forms which may not be 'eval'able.
The namesake feature is interpolating Data::Dumper output into strings, but simple functions are also provided to visualize a scalar, array, or hash.
Diagnostic and debug messages are primary use cases.
Internally, Data::Dumper is called to visualize (i.e. format) data with pre- and postprocessing to "improve" the results:
Output is compact (1 line if possibe, otherwise folded at your terminal width), and OMITS a trailing newline. Unicode characters appear as themselves, objects like Math:BigInt are stringified, "virtual" values behind overloaded array/hash-deref operators are shown, and some Data::Dumper bugs^H^H^H^Hquirks are circumvented. See "DIFFERENCES FROM Data::Dumper".
Finally, a few utilities are provided to quote strings for /bin/sh.
FUNCTIONS
ivis 'string to be interpolated'
Returns the argument with variable references and escapes interpolated as in in Perl double-quotish strings, but using Data::Dumper to format variable values.
$var
is replaced by its value, @var
is replaced by "(comma, sparated, list)", and %hash
by "(key => value, ...)" . Most complex expressions are recognized, e.g. indexing, dereferences, slices, etc.
Expressions are evaluated in the caller's context using Perl's debugger hooks, and may refer to almost any lexical or global visible at the point of call (see "LIMITATIONS").
IMPORTANT: The argument string must be single-quoted to prevent Perl from interpolating it beforehand.
dvis 'string to be interpolated'
Like ivis
with the addition that interpolated expressions are prefixed with a "exprtext=" label.
The 'd' in 'dvis' stands for debugging messages, a frequent use case where brevity of typing is more highly prized than beautiful output.
vis [SCALAREXPR]
avis LIST
hvis EVENLIST
vis
formats a single scalar ($_ if no argument is given) and returns the resulting string.
avis
formats an array (or any list) as comma-separated values in parenthesis.
hvis
formats key => value pairs in parenthesis.
alvis LIST
hlvis EVENLIST
The "l" variants return a bare list without the enclosing parenthesis.
ivisq 'string to be interpolated'
dvisq 'string to be interpolated'
visq [SCALAREXPR]
avisq LIST
hvisq LIST
alvisq LIST
hlvisq EVENLIST
Alternatives with a 'q' suffix display strings in 'single quoted' form if possible.
Internally, Data::Dumper is called with Useqq(0)
, but depending on the version of Data::Dumper the result may be "double quoted" anyway if wide characters are present.
OBJECT-ORIENTED INTERFACES
Data::Dumper::Interp->new()
visnew
Creates an object initialized from the global configuration variables listed below (the function visnew
is simply a shorthand wrapper function).
No arguments are permitted.
The functions described above may then be called as methods on the object (when not called as a method the functions create a new object internally).
For example:
$msg = Data::Dumper::Interp->new()->Foldwidth(40)->avis(@ARGV);
and
$msg = visnew->Foldwidth(40)->avis(@ARGV);
return the same string as
local $Data::Dumper::Interp::Foldwidth = 40;
$msg = avis(@ARGV);
Configuration Variables / Methods
These work the same way as variables/methods in Data::Dumper.
For each configuration value, there is a global variable in package Data::Dumper::Interp
which provides the default, and a method of the same name which sets or retrieves a config value on a specific object.
When a method is called without arguments the current value is returned.
When a method is called with an argument to set a value, the object is returned so that method calls can be chained.
MaxStringwidth(INTEGER)
Truncsuffix("...")
Longer strings are truncated and Truncsuffix appended. MaxStringwidth=0 (the default) means no limit.
Foldwidth(INTEGER)
Defaults to the terminal width at the time of first use.
Overloads(BOOL);
Overloads("classname")
Overloads([ list of classnames ])
A false value disables stringification or evaluation of overloaded deref operators.
A "1" (the default) enables for all objects, otherwise only for the specified class name(s).
If enabled, then objects are checked for an overloaded stringification ('""') operator, or array-, hash-, scalar-, or glob- deref operators, in that order. The first overloaded operator found will be evaluated and the object ref replaced by the result. The check is then repeated.
Sortkeys(subref)
The default sorts numeric substrings in keys by numerical value. See Data::Dumper
documentation.
Useqq
The default is "unicode:controlpic" except for functions/methods with 'q' in their name, which force Useqq(0)
.
0 means generate 'single quoted' strings when possible.
1 means generate "double quoted" strings, as-is from Data::Dumper. Non-ASCII charcters will be shown as hex escapes.
Otherwise generate "double quoted" strings enhanced according to option keywords given as a :-separated list, e.g. Useqq("unicode:controlpic"). The avilable options are:
- "unicode" (or "utf8" for historical reasons)
-
All printable characters are shown as themselves rather than hex escapes, and '\n', '\t', etc. are shown for common ASCII control codes.
- "controlpic"
-
Show ASCII control characters using single "control picture" characters, for example '' is shown for newline instead of '\n'. Similarly for \0 \a \b \e \f \r and \t.
This is sometimes useful for debugging because every character occupies the same space with a fixed-width font. The commonly-used "Last Resort" font for these characters can be hard to read on modern high-res displays. You can set
Useqq
to just "unicode" to see traditional \n etc. backslash escapes while still seeing wide characters as themselves. - "qq"
- "qq=XY"
-
Show using Perl's qq{...} syntax, or qqX...Y if deliters are specified, rather than "...".
Quotekeys
Sparseseen
Maxdepth
Maxrecurse
Deparse
See Data::Dumper
documentation.
UTILITY FUNCTIONS
u
u SCALAR
Returns the argument ($_ by default) if it is defined, otherwise the string "undef".
quotekey
quotekey SCALAR
Returns the argument ($_ by default) if it is a valid bareword, otherwise a quoted string.
qsh
qsh $string
qshpath
qshpath $might_have_tilde_prefix
The string ($_ by default) is quoted if necessary for parsing by /bin/sh, which has different quoting rules than Perl.
If the string contains only "shell-safe" ASCII characters it is returned as-is, without quotes.
qshpath
is like qsh
except that an initial ~ or ~username is left unquoted. Useful for paths given to bash or csh.
If the argument is a ref but is not an object which stringifies, then vis() is called and the resulting string quoted. An undefined value is shown as undef
without quotes; as a special case to avoid ambiguity the string 'undef' is always "quoted".
LIMITATIONS
- Interpolated Strings
-
ivis
anddvis
evaluate expressions in the user's context using Perl's debugger support ('eval' in package DB -- see perlfunc). This mechanism has some limitations:@_ will appear to have the original arguments to a sub even if "shift" has been executed. However if @_ is entirely replaced, the correct values will be displayed.
A lexical ("my") sub creates a closure, and variables in visible scopes which are not actually referenced by your code may not exist in the closure; an attempt to display them with
ivis
will fail. For example:our $global; sub outerfunc { my sub inner { say dvis '$global'; # croaks with "Error interpolating '$global'" # my $x = $global; # ... unless this is un-commented } &inner(); } &outerfunc;
- Multiply-referenced items
-
If a structure contains several refs to the same item, the first ref will be visualized by showing the referenced item as you might expect.
However subsequent refs will look like
$VAR1->place
whereplace
is the location of the first ref in the overall structure. This is how Data::Dumper indicates that the ref is a copy of the first ref and thus points to the same datum. "$VAR1" is an artifact of how Data::Dumper would generate code using its "Purity" feature. Data::Dumper::Interp does nothing special and simply passes through these annotations. - The special "_" stat filehandle may not be preserved
-
Data::Dumper::Interp queries the operating system to obtain the window size to initialize
$Foldwidth
, if it is not already defined; this may change the "_" filehandle. After the first call (or if you pre-set$Foldwidth
), the "_" filehandle will not change across calls.
DIFFERENCES FROM Data::Dumper
Results differ from plain Data::Dumper
output in the following ways (most substitutions can be disabled via Config options):
A final newline is not included.
Everything is shown on a single line if possible, otherwise wrapped to the terminal width with indentation appropriate to structure levels.
Printable Unicode characters appear as themselves instead of \x{ABCD}.
Note: If your data contains 'wide characters', you must encode the result before displaying it as explained in
perluniintro
, for example withuse open IO => ':locale';
. You'll also wantuse utf8;
if your Perl source contains characters outside the ASCII range.Undecoded binary octets (e.g. data read from a 'binmode' file) will be escaped as individual bytes when necessary.
'' and similar "control picture" characters are shown for ASCII controls.
Object refs are replaced by the object's stringified representation. For example,
bignum
andbigrat
numbers are shown as easily readable values rather than "bless( {...}, 'Math::...')".Stingified objects are prefixed with "(classname)" to make clear what happened.
The "virtual" value of objects which overload a dereference operator (
@{}
or%{}
) is displayed instead of the object's internals.Hash keys are sorted treating numeric "components" numerically. For example "A.20" sorts before "A.100".
All punctuation variables, including $@ and $?, are preserved over calls.
Representation of numbers and strings are made predictable and obvious: Floating-point values always appear as numbers (not 'quoted strings'), and strings containing digits like "42" appear as quoted strings and not numbers (string vs. number detection is ala JSON::PP).
Although such differences might be immaterial to Perl when executing code, they may be important when communicating to a human.
SEE ALSO
Data::Dumper
Jim Avera (jim.avera AT gmail)