in reply to why is this code bad under -w option?

A bit late, I imagine, but here's another way of doing it if the aim, as it appears to be, is just to convert the input string into a number:

use strict; use warnings; my %convert = ( KB => 10, MB => 20, GB => 30 ); my $num; { print "Enter the block size\n (example: 400KB,10MB,1GB): "; my $block_size = uc <STDIN>; print "Invalid block type specified!\n\n" and redo unless $block_size =~ /^(\d+)([KMG]B)$/; $num = $1 * 2 ** $convert{$2}; } print $num;

dave

Replies are listed 'Best First'.
Re: Re: why is this code bad under -w option?
by Grygonos (Chaplain) on Jul 28, 2003 at 18:31 UTC
    thanks dave... that's a super cool way of doing it...

      Just after posting, I realised that I could make it more maintainable:

      my %convert = ( KB => 10, MB => 20, GB => 30 ); my $num; { print "Enter the block size\n (example: 400KB, 10MB, 1GB): "; my $block_size = uc <STDIN>; print "Invalid block type specified!\n\n" and redo unless $block_size =~ /^(\d+)([A-Z]B)$/ # Changed and defined $convert{$2}; # here $num = $1 * 2 ** $convert{$2}; } print $num;

      That way, it could be more easily be adapted to include terabytes, by just adding TB => 50 to the %convert hash, and then, a few months later :), the same for whatever comes after terabytes...

      dave