Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^6: Not A Rockstar File Manipulator Today

by TGI (Parson)
on Nov 16, 2008 at 19:37 UTC ( #723944=note: print w/replies, xml ) Need Help??

in reply to Re^5: Not A Rockstar File Manipulator Today
in thread Not A Rockstar File Manipulator Today

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

Replies are listed 'Best First'.
Re^7: Not A Rockstar File Manipulator Today
by ptoulis (Scribe) on Nov 16, 2008 at 23:21 UTC
    Thanks TGI, I can clearly see your point now. Indeed, 0 is perfectly possible user input, but evaluates to false in my test! A perfect way to get fired!

    Oh, about the Camel, it has never left my desk the last 2 months! My brother bought it to me as a present, and I am reading every inch of it. It is not only the language details, but programming concepts in general that make this book a real treasure. It is a misconception that scripting languages like Perl do harm to programmers. This book has enlightened me on the contrary.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://723944]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-02-23 19:24 GMT
Find Nodes?
    Voting Booth?
    I use postfix dereferencing ...

    Results (120 votes). Check out past polls.