++ed by:
DCPETROV KEEDI

2 PAUSE users

Adriano Ferreira

NAME

Number::Bytes::Human - Convert byte count to human readable format

SYNOPSIS

  use Number::Bytes::Human qw(format_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'

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

DESCRIPTION

THIS IS ALPHA SOFTWARE: THE DOCUMENTATION AND THE CODE WILL SUFFER CHANGES VERY SOON (THANKS, GOD!).

This module provides a formatter which turns byte counts to usual readable format, like '2.0K', '3.1G', '100B'.

  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/

OPTIONS

BASE
  block | base | block_size | bs => 1000 | 1024
  base_1024 | block_1024 | 1024 => 1
  base_1000 | block_1000 | 1000 => 1

The base to be used: 1024 (default) or 1000.

Any other value throws an exception.

ZERO
  zero => string | undef

The string 0 maps to ('0' by default). If undef, the general case is used. The string may contain '%S' in which case the suffix for byte is used.

  format_bytes(0, zero => '-') => '-'
METRIC SYSTEM
  si => 1
ROUND
  round_style => 'ceil' | 'floor'
  round_function => $coderef
TO_S
QUIET
  quiet => 1

Suppresses the warnings emitted. Currently, the only case is when the number is large than $base**(@suffixes+1).

OBJECTS

An alternative to the functional style of this module is the OO 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);
    ...
  }

vs.

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

MAKE IT JUST A MATTER OF STYLE: memoize _parse_args() for $seed == undef

EXPORT

It is alright to import format_bytes, but nothingis exported by default.

DIAGNOSTICS

  "unknown round style '$style'";

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

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

  "suffixes ($args{suffixes}) should be an array ref";

  "negative numbers are not allowed" (??)

SEE ALSO

lib/human.c and lib/human.h in GNU coreutils.

The _convert() solution by COG in Filesys::DiskUsage.

BUGS

Please report bugs via CPAN RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Bytes-Human.

AUTHOR

Adriano R. Ferreira, <ferreira@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2005 by Adriano R. Ferreira

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