PBP: 039 Long Numbers

Great big numbers full of digits are hard to read and be sure they’re right, so the PBP suggest using the magic underscore in the numbers to spread them out and make them more readable.

And it is true, it helps:


$money = 123000000000000;

$cost = 111111111111111111;

Can you afford it?

$money = 123_000_000_000_000;
$cost  = 111_111_111_111_111_111;

Apparently not.

The book also points out that before Perl 5.8 you could only put an _ every three characters, and only in integers.  Later perls can put them between any two digits, including in floating point and non-decimal values.  Like in hex:


$netmask = 0xFF_FF_FE_00;

$junk = 0xDEAD_BEEF;

I agree with this suggestion, and even occasionally remember to do it.  I usually forget until I wind up dealing with tens of millions.

Leave a Reply