in reply to Re: why is this code bad under -w option?
in thread why is this code bad under -w option?
Also, get rid of the $valid_block_type crud.
This way, you defer all the calculation until you actually use it. You also get rid of excess variables and you can treat your block information as a whole instead of three related variables. (Anytime two things are related, make that relationship explicit with either an array or hash. Don't use parallel naming to tell your reader what's going on.)use strict; use warnings; my %is_valid_type = do { my $i = 1; map { $_ => 10 * $i++ } qw( KB MB GB ) }; print "Enter the pathname and filename of the file to split: "; my $filename = <STDIN>; chomp($filename); my %block = map { $_ => undef } qw( size type amount ); while (1) { print "Enter the block size \n (example: 400KB,10MB,1GB): "; chomp($block{size} = <STDIN>); @block{qw(amount type)} = ($block{size} =~ m/^\s*([\d.]+)\s*([a-zA +-Z]{2})\s*$/); $block{type} ||= 'KB'; $block{type} = uc $block_type; unless ( $is_valid_type{$block{type}} ) { print "Invalid block type specified!\n\n"; next; } $block{size} = 2 ** $is_valid_type{$block{type}}; last; }
Also, you weren't storing the amount of KB that the user want to work with. Did you even write a spec for this? This code looks quite spec-less ...
------
We are the carpenters and bricklayers of the Information Age.
Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: why is this code bad under -w option?
by halley (Prior) on Jul 28, 2003 at 17:20 UTC | |
by dragonchild (Archbishop) on Jul 28, 2003 at 17:46 UTC | |
|
Re: Re: Re: why is this code bad under -w option?
by Grygonos (Chaplain) on Jul 28, 2003 at 17:25 UTC | |
by dragonchild (Archbishop) on Jul 28, 2003 at 17:52 UTC | |
by Grygonos (Chaplain) on Jul 28, 2003 at 18:02 UTC |