Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

The thing is there is no reason to sort the hash keys, if you iterate over the list of keys you used to generate the settings hash, the keys will be displayed in the order you specify them. So if you enter the keys in alphabetical order, then you get them back in that order.

There is no reason to initialize the hash values to prepare for logical testing. Multiple values in perl are false, '', 0, and undef are all false. This leads to a good reason not to initialize your hash. What would happen if 0 is a legal value for one of your parameters? Your test would fail. It's safer to test for definedness: if( defined $settings{output} ) { do stuff }. Forgetting that 0 is a defined but false value is a distressingly common bug in perl code. Testing if a variable is defined is a common task. The commonness of this test lead to the addition of the defined or operator (//) in perl 5.10.

So, testing for definedness is good, but in the context of this code it is useless because the loop that sets the parameters initializes every key to a string of 0 or more characters. In this case it makes more sense to test for string length: if( length $settings{output} ) { do stuff }. If you didn't care if 0 was rejected, the empty string is false, so you still don't need to initialize to 0.  if( '' ) { never runs }

Now, you may be thinking that having to worry about do I need to test or length or definedness or whatever gets difficult if you have a big program. And that It would be nice to just remember to test for definedness. But the cursed loop ruins that approach since it initializes everything. The solution is simple:

for my $key (@params) { print "$key = "; my $value = <>; chomp $value; $settings{$key} = length $value ? $value : undef; }

Now you have a nice, expected undef value to look for. The great thing about this, is that if you have warnings enabled, if you try to use the undef value in a string or math operation, you'll get a warning. This provides a handy alert that you forgot to validate some input or other.

This technique works along the same lines as the redo I used in my second response to you.

If you've got safari access, I have no qualms in suggesting the Camel (Programming Perl) as well. BTW, don't just swallow every bit of advice in PBP. There has been a lot of discussion here and elsewhere about the various topics in the book.


TGI says moo


In reply to Re^6: 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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (6)
    As of 2014-09-23 07:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (210 votes), past polls