I need get size of array in perl, but size must to be in bytes. I can not find any function for this. Thanks in advance
-
This is a very vague question. Are you after the resident size in memory of just the array itself? The array and its values too? What if those values are themselves references - do you count the data it refers to as well? Or maybe you're more after the number of bytes that would be output if you were to serialise it in some form or other?LeoNerd– LeoNerd2014-12-07 15:13:21 +00:00Commented Dec 7, 2014 at 15:13
Add a comment
|
1 Answer
You can do this using the Devel::Size module -> https://metacpan.org/pod/Devel::Size
#!/usr/bin/perl
use strict;
use warnings;
use Devel::Size qw(total_size);
my @arr = (1, 2, 3, "Foo", "Bar", "Baz", [4, 5, 6], {xyz => 2048});
print "Size: ", total_size(\@arr), " bytes.\n";
On my system this prints:
bria@hel:~$ ./size.pl
Size: 765 bytes.
bria@hel:~$