The second take is much more readable. But you are doing some extra, unnecessary work when you initialize your settings hash. What does setting each key to '0' do for you? It's easier to let perl create the keys in the loop as it goes. Also, why sort the keys of the settings hash. Sort may be a simple looking command, but it can get expensive to sort big arrays. Just put your params array in the order you want to use. So you wind up iterating over your param array twice, and sorting it once.
Any setting value will always be defined, as at least the empty string, so there is no need to initialize your hash values.
foreach my $key (@params) {
print "$key = ";
my $value = <>; # must at minimum be "\n"
chomp $value; # must at minimum be ""
redo unless length $value; # rerun loop if value is empty string
+.
}
Also the or between checking for the output file and the opening it is unnecessary. If you die from the open failing, you die, and execution stops. The following is identical in behavior to yours:
-e $settings{output} and die "File Exists..will not override!";
open(OUT,">",$settings{output}) or die "Error while opening file: $!";
It's amazing how you can do more with less in perl.
I think "The Right Thing To Do"tm as we end the script is to check the results of the handle closes.
close IN or warn "Error closing input $!\n";
close OUT or warn "Error closing output $!\n";
Perl is a lot of fun to play with. When I first started I went crazy for the ternary operator ($foo = $bar > 3 ? 'BIG' : 'small';) and used for all sorts of things. Keep playing and experimenting. You will learn from it. Keep your old code and come back and look at it months later. You'd be amazed at what you learn from this, too.
If you can manage it on a student budget pick up PBP. It is an excellent book. It has tons of advice from grizzled veterans who've stepped in many of the traps that you are just discovering. For fun, and if you want to blow your mind, take a look at Higher Order Perl. Dominus does a great job introducing functional programming techniques in perl.
Also, I can't recommend wandering around the The Portland Pattern Repository enough.
Mainly, keep having fun and learning new stuff.
|