Sponsoring The Perl Toolchain Summit 2025: Help make this important event another success Learn more

#!/usr/bin/perl -w
=pod
=head1 NAME
psize - report memory usage for Perl structures
=head1 SYNOPSYS
psize 'Structure as perl code' ['options as string in hash form']
psize '[1, 2, { a => "foo" }'
psize 'Math::BigInt->new(123)' 't => "Total"'
Valid options are:
Boolean options:
Short name Long name Description
a addr print addresses
c class print class names
d doubles extensive cycle detection
(memory intensive)
t total print a total
o overhead print overhead
h head print a header
s summary print a summary
te terse generate terse report
String options:
i indent string to indent with
l left very first indent string
b bytes The string for sizes,
defaults to "bytes"
};
=head1 DESCRIPTION
=cut
use strict;
use Devel::Size::Report qw/report_size/;
if (@ARGV == 0)
{
# no options?
require Pod::Usage;
Pod::Usage::pod2usage(2);
}
my $token = shift || '"A scalar"';
my $options = shift || {};
eval (" \$options = { $options }") if !ref $options;
my $map = {
a => 'addr',
b => 'bytes',
c => 'class',
d => 'doubles',
t => 'total',
o => 'overhead',
i => 'indend',
l => 'left',
h => 'head',
s => 'summary',
te => 'terse',
};
# map 'a' => 'addr'
my $opt = {};
foreach my $key ( keys %$options )
{
my $m = $key; $m = $map->{$key} if exists $map->{$key};
$opt->{$m} = $options->{$key};
}
$opt->{head} =
"Size report v$Devel::Size::Report::VERSION for '$token' =>";
my $eval = $token;
# make "Foo->new(1)" => "use Foo; Foo->new(1)"
$eval = "use $1; $token" if $token =~ /^\s*([\w:]+)->/;
print report_size ( eval $eval, $opt );