Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^5: Not A Rockstar File Manipulator Today

by ptoulis (Scribe)
on Nov 16, 2008 at 15:50 UTC ( #723931=note: print w/replies, xml ) Need Help??

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

Hi TGI, about sorting the settings hash, I thought the user should be prompted first for input then for output,then pattern, replacement , which happens to be the alphabetical order of the problem parameters! Also I set the initial value to 0 because we could use a simple if to test the settings (e.g. if($settings{output}) {open...}.

Your links are great! Safari has an online version for 'Best Practices' and looks like a great book. 'Higher Order Perl' needs you to have stomach, but I am totally in for it. Perl has some much flexible and expressive structures that make functional programming really really relevant.

Replies are listed 'Best First'.
Re^6: Not A Rockstar File Manipulator Today
by TGI (Parson) on Nov 16, 2008 at 19:37 UTC

    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

      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://723931]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-03-23 04:45 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (281 votes). Check out past polls.