Datify - Simple stringification of data.
version 0.14.163
use Datify; my $datify = Datify->new( ... ); # See OPTIONS below $datify = $datify->set( ... ); # See OPTIONS below print $datify->varify( data => [...] ), "\n"; # "@data = (...);\n" # Or print Datify->varify( data => [...] ), "\n"; # "@data = (...);\n"
Datify is very similar to Data::Dumper, except that it's easier to use, and has better formatting and options.
Datify
The neame of the default variable. This is also set as the first parameter to varify.
varify
What an assignment statement should look like. If the generated code is to be run under use strict;, then you may want to change this to 'my $var = $value;'.
use strict;
'my $var = $value;'
The delimiters for a list.
Set this to a CODE reference that you would like to use to beautify the code. It should accept the code as the first parameter, process it, and return the code after all the beauty modifications have been completed.
CODE
An example:
use Datify; use Perl::Tidy; sub beautify { my $source = shift; my ($dest, $stderr); Perl::Tidy::perltidy( argv => [ qw( --noprofile --nostandard-output --standard-error-output --nopass-version-line ) ], source => \$source, destination => \$dest, stderr => \$stderr, errorfile => \$stderr, ) && die $stderr; return $dest; } Datify->set( beautify => \&beautify ); say Datify->varify( var => $var );
What to use as the null value.
What to use as the values for true and false, respectively. Since Perl does not have native boolean values, these are placeholders.
true
false
What to use as the default quote character. If set to a false value, then use the best guess. See "stringify( value )".
The default single-quoting character.
The default double-quoting character.
The special single-quoting character starter.
The special double-quoting character starter.
The characters in a double quoted sting that need to be quoted, or they may be interpreted as variable interpolation.
How long a string needs to be before it's considered long. See "stringify( value )". Change to a false value to indicate no string is long. Change to a negative value to indicate every string is long.
How to encode characters that need encoding.
When determining the quote character to use, go through these lists to see which character would work best.
What character to use to seperate sets of numbers.
How to generate a LValue.
The formatting string to use. If vsep is set, then vformat should use the * format to inidicate what vsep will be: vformat => 'v%*vd', vsep => '.'.
*
vformat => 'v%*vd', vsep => '.'
The representation of an array reference.
The representation of the separator between list elements.
The representation of a hash reference.
The representation of a pair.
How to sort the keys in a hash. This has a performance hit, but it makes the output much more readable. See the description of "keysort" below.
Any keywords that should be quoted, even though they may not need to be.
The list of overloads to check for before deconstructing the object. See overload for more information on overloading.
The representation of an object. Other possibilities include '$class($data)' or '$class->new($data)'.
'$class($data)'
'$class->new($data)'
The representation of unknown IO objects.
The representation of a code reference. This module does not currently support decompiling code to make a complete representation, but if passed a representation, can wrap it in this.
The representation of the body to a code reference. This module does not currently support decompiling code to make a complete representation.
The representation of a reference.
The representation of dereferencing.
The representation of dereferencing a nested reference.
The representation of a format. This module does not currently support showing the acutal representation.
add_handler( 'Class::Name' => \&code_ref )
Add a handler to handle an object of type 'Class::Name'. \&code_ref should take two parameters, a reference to Datify, and the object to be Datify'ed. It should return a representation of the object.
'Class::Name'
\&code_ref
# Set URI's to stringify as "URI->new('http://example.com')" # instead of "bless(\'http://example.com', 'URI')" Datify->add_handler( 'URI' => sub { my ( $datify, $uri ) = @_; my $s = $datify->stringify("$uri"); return "URI->new($s)"; } );
new( name => value, name => value, ... )
Create a Datify object with the following options.
See "OPTIONS" for a description of the options and their default values.
set( name => value, name => value, ... )
Change the "OPTIONS" settings. When called as a class method, changes default options. When called as an object method, changes the settings and returns a new object.
NOTE: When called as a object method, this returns a new instance with the values set, so you will need to capture the return if you'd like to persist the change:
$datify = $datify->set( ... );
get( name, name, ... )
Get one or more existing values for one or more settings. If passed no names, returns all parameters and values.
Can be called as a class method or an object method.
varify( name => value, value, ... )
Returns an assignment statement for the values. If name does not begin with a sigil ($, @, or %), will determine which sigil to use based on values.
name
$
@
%
values
Some examples:
Common case, determine the type and add the correct sigil to 'foo'.
Datify->varify( foo => $foo )
Specify the type.
Datify->varify( '$foo' => $foo )
Handle a list: @foo = (1, 2, 3);
@foo = (1, 2, 3);
Datify->varify( '@foo' => 1, 2, 3 ) Datify->varify( '@foo' => [ 1, 2, 3 ] ) Datify->varify( foo => 1, 2, 3 ) Datify->varify( foo => [ 1, 2, 3 ] )
Handle a hash: %foo = (a => 1, b => 2, c => 3); (Note: Order may be rearranged.)
%foo = (a => 1, b => 2, c => 3);
Datify->varify( '%foo' => a => 1, b => 2, c => 3 ) Datify->varify( '%foo' => { a => 1, b => 2, c => 3 } ) Datify->varify( foo => { a => 1, b => 2, c => 3 } )
Keep in mind that without proper hints, this would be interpretted as a list, not a hash:
Datify->varify( foo => a => 1, b => 2, c => 3 ) # "@foo = ('a', 1, 'b', 2, 'c', 3);"
undefify
Returns the string that should be used for an undef value.
booleanify( value )
Returns the string that represents the true or false interpretation of value.
stringify1( value , delimiters )
Returns the string that represents value as a single-quoted string. The delimiters parameter is optional.
stringify2( value , delimiters )
Returns the string that represents value as a double-quoted string. The delimiters parameter is optional.
stringify( value )
Returns the string the represents value. It will be a double-quoted string if it is longer than the longstr option or contains control characters. It will be a single-quoted string unless there are single-quotes within the string, then it will be a double-quoted string, unless it also contains double-quotes within the string, then it will attempt to find the best quote character.
longstr
numify( value )
Returns value with seperators between the hundreds and thousands, hundred-thousands and millions, etc. Similarly for the fractional parts.
Datify->numify(1234.5678901) # "1_234.56_789_01"
scalarify( value )
Returns value as a scalar. If value is not a reference, performs some magic to correctly print vstrings and numbers, otherwise assumes it's a string. If value is a reference, hands off to the correct function to create the string.
Handles reference loops.
lvalueify( value )
Returns an approximate representation of what the lvalue is.
vstringify( value )
A representation of the VString, in dotted notation.
regexpify( value, delimiters )
A representation of the Regexp in value.
Regexp
value
listify( value, value, ... )
Returns value(s) as a list.
Datify->listify( 1, 2, 3 ) # '1, 2, 3'
arrayify( value, value, ... )
Returns value(s) as an array.
Datify->arrayify( 1, 2, 3 ) # '[1, 2, 3]'
keyify( value )
Returns value as a key. If value does not need to be quoted, it will not be. Verifies that value is not a keyword.
pairify( value, value, ... )
Returns value(s) as a pair.
Datify->pairify( a => 1, b => 2 ) # 'a => 1, b => 2'
hashify( value, value, ... )
Returns value(s) as a hash.
Datify->hashify( a => 1, b => 2 ) # '{a => 1, b => 2}'
overloaded( $object )
Returns the first method from the overloads list that $object has overloaded. If nothing is overloaded, then return nothing.
overloads
objectify( value )
Returns value as an object.
Datify->objectify( $object ) # "bless({}, 'Object')"
ioify( value )
Returns a representation of value that is accurate if value is STDIN, STDOUT, or STDERR. Otherwise, returns the io setting.
io
codeify( value )
Returns a subroutine definition that is not likely to encode value.
Datify->codeify( \&subroutine ) # 'sub {...}'
However, if value is a string, then wrap that string with code, or if value is a reference to something other than CODE, represent that reference by wrapping it with code.
code
refify( value )
Returns value as reference.
formatify( value )
Returns a value that is not completely unlike value.
globify( value )
Returns a representation of value. For normal values, remove the leading main::.
main::
keysort
Not a method, but a sorting routine that sorts numbers (using <=>) before strings (using cmp).
<=>
cmp
Please report any bugs or feature requests on the bugtracker website https://github.com/rkleemann/Datify/issues or by email to bug-Datify@rt.cpan.org.
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Handle formats better.
Data::Dumper
Bob Kleemann <bobk@cpan.org>
This software is Copyright (c) 2014 by Bob Kleemann.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Datify, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Datify
CPAN shell
perl -MCPAN -e shell install Datify
For more information on module installation, please visit the detailed CPAN module installation guide.