in reply to why is this code bad under -w option?
The reason this is better, is because now you only have a single branch of logic, and you can now control this logic in one place instead of two. Anytime you have the oppurtunity to control program flow/logic at a single point in the code, take it. This approach makes the code much more maintainable.#!/usr/bin/perl -w use strict; use IO::File; my %multipliers = ( 'KB' => 2**10, 'MB' => 2**20, 'GB' => 2**30, ); print "Enter the pathname and filename of the file to split: "; my $filename = <STDIN>; my $valid_block_type; my $block_size; my $block_type; until($valid_block_type) { print "Enter the block size \n (example: 400KB,10MB,1GB): "; $block_size = <STDIN>; chomp($block_size); ($block_type) = ($block_size =~ m/([a-zA-Z]{2})$/); $block_type ||= 'KB'; $block_type = uc $block_type; if( grep($block_type eq $_, keys %multipliers) ) { $block_type = $multipliers{$block_type}; $valid_block_type = 1; } else { print "Invalid block type specified!\n\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: why is this code bad under -w option?
by dragonchild (Archbishop) on Jul 28, 2003 at 16:50 UTC | |
by halley (Prior) on Jul 28, 2003 at 17:20 UTC | |
by dragonchild (Archbishop) on Jul 28, 2003 at 17:46 UTC | |
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 |