in reply to [OT]: Re^9: What's the right way to write a method which returns one line at a time from a file?
in thread What's the right way to write a method which returns one line at a time from a file?
Yes, I agree. I especially like that there is only one return statement. In contrast, using multiple return statements violates the principle of DRY.
Related items from Conway's Perl Best Practices, Chapter 6 (Control Structures):
- Avoid cascading an if
- Use table look-up in preference to cascaded equality tests
- When producing a value, use tabular ternaries
Conway recommends formatting tabular ternaries with the colon at the front, for example:
because, if you squint, it looks like a lookup table, thus converting the notoriously impenetrable ternary syntax into a familiar and easy to understand lookup table.# When their name is... Address them as... my $salute = $name eq $EMPTY_STR ? 'Customer' : $name =~ m/(.*), \s+ Ph[.]?D \z /xms ? "Dr $1" : $name ;
Update: Fixed typo in Conway's example my $salute statement above (removed extra ?). Thanks AnomalousMonk.
In Section
Seekers of Perl Wisdom