Wednesday, August 6, 2008

Number::Bytes::Human

I came across a useful Perl module today called Number::Bytes::Human. I occassionally download files from the newsgroups using nzbperl, which tells me once it's started how big the download is expected to be. That's all well and good, but what if I want to find that out before starting up nzbperl?

I decided to write a quick script to extract the byte size from the .nzb file, but it occurred to me halfway through writing it that I didn't want to spend the brainpower this evening to make the output human readable (megabytes and gigabytes, as opposed to bytes). CPAN to the rescue! My script isn't long, so I thought I'd go ahead and post it here. I saved it as ~/bin/nzbsize, but you can call it whatever you want.


#!/usr/bin/perl

use strict;
use Number::Bytes::Human qw(format_bytes);

my $file = slurp( $ARGV[0] );
my $bytes;

while ( $file =~ s{bytes="(.*?)"}{} ) {
$bytes += $1;
}
my $hr = format_bytes( $bytes );

print "Bytes: $bytes\n";
print "Human Readable: $hr\n";
exit;

# Since I'm not running Perl 5.10 just yet
sub slurp {
my ( $file_name ) = @_;
local $/;
open INPUT, "<$file_name";
return <INPUT>;
}


The output's going to look something of like this:


jhall@bourdain nzb$ nzbsize Knoppix5.1.1.nzb
Bytes: 782635787
Human Readable: 747M
jhall@bourdain nzb$


Update: instead of using this version, you probably want to check out the optimized version.

1 comment:

  1. For the daisy flowers on the cake I made I used a daisy cutter, and rolled out a fondant/gumpaste mix really thin. I placed the cut out on thin foam and used the veining tool to vein each petal from the tip to the center (doing this automatically makes the petals curve upward). Using clear vanilla I stacked 2 cutouts together and placed them in a flower former to dry. I made the dot in the center by making a ball of the lighter color and attaching it with clear vanilla. I'm glad you like the flowers on the cake. It was fun to make.

    ReplyDelete

Comments for posts over 14 days are moderated

Note: Only a member of this blog may post a comment.