in reply to Re: Why is the size even bigger after pack?
in thread Why is the size even bigger after pack?

What's the point of pack if it takes more memory after packing?

Replies are listed 'Best First'.
Re^3: Why is the size even bigger after pack?
by chromatic (Archbishop) on Nov 03, 2011 at 06:08 UTC

    What did you think it would do?

    pack turns a list of values into a string according to a specific format. If you want to interact with something else which expects values in a specific format (a system call, a foreign function, a binary protocol), pack is your tool. If you want to use less memory, you need something else.


    Improve your skills with Modern Perl: the free book.

Re^3: Why is the size even bigger after pack?
by Tux (Canon) on Nov 03, 2011 at 07:38 UTC

    Your example only shows one single case. An SV can hold IV's as well as PV's at the same time and a PV representation of 1 takes less space than that of big numbers. YMMV (even with different versions of perl):

    $ cat test.pl use v5.12; use Devel::Size "total_size"; for my $num (1, 1000, 100_000_000) { say "Value $num:"; say total_size ($num); say total_size (pack "w*", $num); } for my $num ("1", "1000", "100_000_000") { say "Value $num:"; say total_size ($num); say total_size (pack "w*", $num); } $ perl test.pl Value 1: 64 48 Value 1000: 64 48 Value 100000000: 72 48 Value 1: 48 48 Value 1000: 48 48 Value 100_000_000: 56 48 $

    Enjoy, Have FUN! H.Merijn