Color me crazy, but why do you want to spend the time calculating 2**30 when you may not even use it?!? That's just a waste of time and space.

Also, get rid of the $valid_block_type crud.

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; }
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.)

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.


In reply to Re: Re: why is this code bad under -w option? by dragonchild
in thread why is this code bad under -w option? by Grygonos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.