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.


TGI says moo


In reply to Re^4: Not A Rockstar File Manipulator Today by TGI
in thread Not A Rockstar File Manipulator Today by koolgirl

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.