SYNOPSIS

  use Number::Bytes::Human qw(format_bytes parse_bytes);
  $size = format_bytes(0); # '0'
  $size = format_bytes(2*1024); # '2.0K'

  $size = format_bytes(1_234_890, bs => 1000); # '1.3M'
  $size = format_bytes(1E9, bs => 1000); # '1.0G'

  my $bytes = parse_bytes('1.0K');   # 1024
  my $bytes = parse_bytes('1.0KB');  # 1000, SI unit
  my $bytes = parse_bytes('1.0KiB'); # 1024, SI unit

  # the OO way
  $human = Number::Bytes::Human->new(bs => 1000, si => 1);
  $size = $human->format(1E7); # '10MB'

  $bytes = $human->parse('10MB');   # 10*1000*1000
  $bytes = $human->parse('10MiB');  # 10*1024*1024
  $bytes = $human->parse('10M');    # Error, no SI unit

  $human->set_options(zero => '-');
  $size = $human->format(0);    # '-'
  $bytes = $human->parse('-');  # 0

  $human = Number::Bytes::Human->new(bs => 1000, round_style => 'round', precision => 2);
  $size = $human->format(10240000); # '10.24MB'

DESCRIPTION

\s-1THIS\s0 \s-1IS\s0 \s-1ALPHA\s0 \s-1SOFTWARE:\s0 \s-1THE\s0 \s-1DOCUMENTATION\s0 \s-1AND\s0 \s-1THE\s0 \s-1CODE\s0 \s-1WILL\s0 \s-1SUFFER\s0 \s-1CHANGES\s0 \s-1SOME\s0 \s-1DAY\s0 (\s-1THANKS\s0, \s-1GOD\s0!).

This module provides a formatter which turns byte counts to usual readable format, like '2.0K', '3.1G', '100B'. It was inspired in the \*(C`-h\*(C' option of Unix utilities like \*(C`du\*(C', \*(C`df\*(C' and \*(C`ls\*(C' for \*(L"human-readable\*(R" output.

From the FreeBSD man page of \*(C`df\*(C': http://www.freebsd.org/cgi/man.cgi?query=df

"Human-readable" output. Use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to four or fewer using base 2 for sizes.

byte B kilobyte K = 2**10 B = 1024 B megabyte M = 2**20 B = 1024 * 1024 B gigabyte G = 2**30 B = 1024 * 1024 * 1024 B terabyte T = 2**40 B = 1024 * 1024 * 1024 * 1024 B

petabyte P = 2**50 B = 1024 * 1024 * 1024 * 1024 * 1024 B exabyte E = 2**60 B = 1024 * 1024 * 1024 * 1024 * 1024 * 1024 B zettabyte Z = 2**70 B = 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 B yottabyte Y = 2**80 B = 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 B

I have found this link to be quite useful:

http://www.t1shopper.com/tools/calculate/

If you feel like a hard-drive manufacturer, you can start counting bytes by powers of 1000 (instead of the generous 1024). Just use \*(C`bs => 1000\*(C'.

But if you are a floppy disk manufacturer and want to start counting in units of 1024000 (for your \*(L"1.44 \s-1MB\s0\*(R" disks)? Then use \*(C`bs => 1_024_000\*(C'.

If you feel like a purist academic, you can force the use of metric prefixes according to the Dec 1998 standard by the \s-1IEC\s0. Never mind the units for base 1000 are \*(C`('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')\*(C' and, even worse, the ones for base 1024 are \*(C`('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')\*(C' with the horrible names: bytes, kibibytes, mebibytes, etc. All you have to do is to use \*(C`si => 1\*(C'. Ain't that beautiful the \s-1SI\s0 system? Read about it:

http://physics.nist.gov/cuu/Units/binary.html

You can try a pure Perl "ls -lh"-inspired command with the one-liner, er, two-liner:

$ perl -MNumber::Bytes::Human=format_bytes \ -e 'printf "%5s %s\n", format_bytes(-s), $_ for @ARGV' *

Why to write such a module? Because if people can write such things in C, it can be written much easier in Perl and then reused, refactored, abused. And then, when it is much improved, some brave soul can port it back to C (if only for the warm feeling of painful programming).

It is also possible to parse human readable formatted bytes. The automatic format detection recognizes \s-1SI\s0 units with the blocksizes of 1000 and 1024 respectively and additionally the customary K / M / G etc. with blocksize 1024. When si => 1 is added to the options only \s-1SI\s0 units are recognized. Explicitly specifying a blocksize changes it for all detected units.

\s-1OBJECTS\s0

An alternative to the functional style of this module is the \s-1OO\s0 fashion. This is useful for avoiding the unnecessary parsing of the arguments over and over if you have to format lots of numbers

for (@sizes) { my $fmt_size = format_bytes($_, @args); ... }

versus

my $human = Number::Format::Bytes->new(@args); for (@sizes) { my $fmt_size = $human->format($_); ... }

for \s-1TODO\s0 [\s-1TODO\s0] \s-1MAKE\s0 \s-1IT\s0 \s-1JUST\s0 A \s-1MATTER\s0 \s-1OF\s0 \s-1STYLE:\s0 memoize _parse_args() $seed == undef

\s-1FUNCTIONS\s0

format_bytes

$h_size = format_bytes($size, @options); Turns a byte count (like 1230) to a readable format like '1.3K'. You have a bunch of options to play with. See the section \*(L"\s-1OPTIONS\s0\*(R" to know the details.

parse_bytes

$size = parse_bytes($h_size, @options); Turns a human readable byte count into a number of the equivalent bytes.

\s-1METHODS\s0

new

$h = Number::Bytes::Human->new(@options); The constructor. For details on the arguments, see the section \*(L"\s-1OPTIONS\s0\*(R".

format

$h_size = $h->format($size); Turns a byte count (like 1230) to a readable format like '1.3K'. The statements $h = Number::Bytes::Human->new(@options); $h_size = $h->format($size); are equivalent to \*(C`$h_size = format_bytes($size, @options)\*(C', with only one pass for the option arguments.

parse

$size = $h->parse($h_size) Turns a human readable byte count into the number of bytes. The statements $h = Number::Bytes::Human->new(@options); $size = $h->format($h_size); are equivalent to \*(C`$size = parse_bytes($h_size, @options)\*(C', with only one pass for the option arguments.

set_options

$h->set_options(@options); To alter the options of a \*(C`Number::Bytes::Human\*(C' object. See \*(L"\s-1OPTIONS\s0\*(R".

\s-1OPTIONS\s0

\s-1BASE\s0

block | base | block_size | bs => 1000 | 1024 | 1024000 base_1024 | block_1024 | 1024 => 1 base_1000 | block_1000 | 1000 => 1 The base to be used: 1024 (default), 1000 or 1024000. Any other value throws an exception.

\s-1SUFFIXES\s0

suffixes => 1000 | 1024 | 1024000 | si_1000 | si_1024 | $arrayref By default, the used suffixes stand for '', 'K', 'M', ... for base 1024 and '', 'k', 'M', ... for base 1000 (which are indeed the usual metric prefixes with implied unit as bytes, 'B'). For the weird 1024000 base, suffixes are '', 'M', 'T', etc.

\s-1ZERO\s0

zero => string | undef The string 0 maps to ('0' by default). If \*(C`undef\*(C', the general case is used. The string may contain '%S' in which case the suffix for byte is used. format_bytes(0, zero => '-') => '-'

\s-1METRIC\s0 \s-1SYSTEM\s0

si => 1

\s-1ROUND\s0

round_function => $coderef round_style => 'ceil' | 'floor' | 'round' | 'trunc'

\s-1TO_S\s0
\s-1QUIET\s0

quiet => 1 Suppresses the warnings emitted. Currently, the only case is when the number is large than \*(C`$base**(@suffixes+1)\*(C'.

\s-1PRECISION\s0

precision => <integer> default = 1 sets the precicion of digits, only apropreacte for round_style 'round' or if you want to accept it in as the second parameter to your custome round_function.

\s-1PRECISION_CUTOFF\s0

precision_cutoff => <integer> default = 1 when the number of digits exceeds this number causes the precision to be cutoff (was default behaviour in 0.07 and below)

\s-1EXPORT\s0

It is alright to import \*(C`format_bytes\*(C' and \*(C`parse_bytes\*(C', but nothing is exported by default.

DIAGNOSTICS

"unknown round style '$style'";

"invalid base: $block (should be 1024, 1000 or 1024000)";

"round function ($args{round_function}) should be a code ref";

"suffixes ($args{suffixes}) should be 1000, 1024, 1024000 or an array ref";

"negative numbers are not allowed" (??)

TO DO

A function \*(C`parse_bytes\*(C'

parse_bytes($str, $options)

which transforms '1k' to 1000, '1K' to 1024, '1MB' to 1E6, '1M' to 1024*1024, etc. (like gnu du).

$str =~ /^\s*(\d*\.?\d*)\s*(\S+)/ # $num $suffix

RELATED TO Number::Bytes::Human…

lib/human.c and lib/human.h in \s-1GNU\s0 coreutils.

The \*(C`_convert()\*(C' solution by \s-1COG\s0 in Filesys::DiskUsage.

BUGS

Please report bugs via \s-1CPAN\s0 \s-1RT\s0 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Bytes-Human <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Bytes-Human> or mailto://[email protected] <mailto://[email protected]>. I will not be able to close the bug as BestPractical ignore my claims that I cannot log in, but I will answer anyway.

AUTHOR

Adriano R. Ferreira, <[email protected]>

COPYRIGHT AND LICENSE

Copyright (C) 2005-2007 by Adriano R. Ferreira

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.