NAME
Devel::Size::Report - generate a size report for all elements in a structure
SYNOPSIS
use Devel::Size::Report qw/report_size/;
my $a = [ 8, 9, 7,
[ \"12",
2, 3,
{ a => 'b',
size => 12.2,
h => ['a']
},
sub { 42; },
'rrr'
] ];
print report_size($a, { indend => " " } );
This will print something like this:
Size report v0.04 for 'ARRAY(0x815a430)':
Array 743 bytes (overhead: 84 bytes, 11.31%)
Scalar 16 bytes
Scalar 16 bytes
Scalar 16 bytes
Array 611 bytes (overhead: 96 bytes, 15.71%)
Scalar reference 27 bytes
Scalar 28 bytes
Scalar 28 bytes
Hash 308 bytes (overhead: 180 bytes, 58.44%)
'h' => Array 82 bytes (overhead: 56 bytes, 68.29%)
Scalar 26 bytes
'a' => Scalar 26 bytes
'size' => Scalar 20 bytes
Code 92 bytes
Scalar 32 bytes
Total: 743 bytes
DESCRIPTION
Devel::Size can only report the size of a single element or the total size of a structure (array, hash etc). This module enhances Devel::Size by giving you the ability to generate a full size report for each element in a structure.
You have full control over how the generated text report looks like, and where you want to output it. In addition, the method track_size
allows you to get at the raw data that is used to generate the report for even more flexibility.
METHODS
report_size
my $record = report_size( $reference, $options ) . "\n";
print $record;
Walks the given reference recursively and returns text tree describing the size of each element. $options
is a hash, containing the following optional keys:
names ref to HASH mapping the types to names
This should map S_Scalar to something like "Scalar" etc
indend string to indend different levels with, default is ' '
left indend all text with this at the left side, default is ''
inner indend inner text with this at the left side, default is ' '
total if true, a total size will be printed as last line
bytes name of the size unit, defaults to 'bytes'
head header string, default 'Size report for'
Set to '' to supress header completely
overhead Format string for the overhead, first size in bytes, then
the bytes string (see above) and then the percentage.
The default is:
" (overhead: %i%s, %0.2f%%)"
addr if true, for each element the memory address will be output
class if true, show the class each element was blessed in
entries_per_element
my $entries = entries_per_element();
Returns the number of entries per element that track_size() will generate.
track_size
@elements = track_size( $reference, $level);
Walk the given scalar or reference recursively and returns an array, containing entries_per_element entries for each element in the structure pointed to by $reference
. $reference
can also be a plain scalar.
The entries currently are:
level the indend level
type the type of the element, S_SCALAR, S_HASH, S_ARRAY etc
if (type & S_KEY) != 0, the element is a member of a hash
size size in bytes of the element
overhead if the element is an ARRAY or HASH, contains the overhead
in bytes (size - sum(size of all elements)).
name if type & S_KEY != 0, this contains the name of the hash key
addr memory address of the element
class classname that the element was blessed into, or ''
track_size
calls itself recursively for ARRAY and HASH references.
type_name
$type_number = type($type_name);
Maps a type name (like 'SCALAR') to a type number (lilke S_SCALAR).
WRAP YOUR OWN
If you want to create your own report with different formattings, please use track_size and create a report out of the data you get. Look at the source code for report_size how to do this - it is easy!
CAVEATS
The limitations of Devel::Size also apply to this module. This means that CODE refs and other "obscure" things might show wrong sizes, or unknown overhead. In addition, some sizes might be reported wrong.
A string representation of the passed argument will be inserted when generating a report with a header.
If the passed argument is an object with overloaded magic, then the routine for stringification will be triggered. If this routine does actually modify the object (for instance, Math::String objects cache their string form upon first call to stringification, thus modifying themselves), the reported size will be different from a first report without a header.
BUGS
Apart from the problems with Devel::Size, none known so far.
AUTHOR
(c) 2004 by Tels http://bloodgate.com