Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In one word: <strong>NO!</strong></p> <p>Let's look at:</p> <pre><code>sub ucwords{ $str = @_[0]; $str = lc($str); $str =~ s/\b(\w)/\u$1/g; return $str; } </code></pre> <p>First and foremost, you are not using <code>strict</code>. Use it. It is for your own good.</p> <p>Second, you are not using <code>warnings</code>. Use it. It is for your own good. For example, the first element of <code>@_</code> should be referred to using <code>$_[0]</code> and <strong>not</strong> <code>@_[0]</code>.</p> <p>Third, you should get in the habit of reading the FAQ list occasionally before re-inventing the wheel again: See <a href="http://perldoc.perl.org/perlfaq4.html#How-do-I-capitalize-all-the-words-on-one-line%3f" rel="nofollow noreferrer">How do I capitalize all the words on one line?</a></p> <p>If you think this is harsh, consider the fact that when called as:</p> <pre><code>print ucwords("FRED AND BARNEY'S LODGE"), "\n"; </code></pre> <p>your code outputs</p> <pre> Fred And Barney'S Lodge </pre> <p>which is the example given in that question.</p> <p>Further, having a function that does more than one thing, chooses what it does on the basis of mystery numbers and does none of those things right is not a good design strategy.</p> <p>You should instead have multiple functions, named in a way that can be understood by a casual reader of your code, each of which does only one thing and does that right.</p> <p>Finally, the extended version of your function (without saying anything about the wisdom of writing such a function) can be better written as:</p> <pre><code># untested code follows use Carp; { my %modes = map {$_ =&gt; undef} 0 .. 3; sub ucwords{ croak 'No arguments passed' unless @_; my ($str, $mode) = @_; $mode = 0 unless defined $mode; croak "Invalid mode: '$mode'" unless exists $modes{$mode}; if ( $mode == 0 ) { $str = lc($str); $str =~ s/\b(\w)/\u$1/g; } elsif ( $mode == 1 ) { $str =~ s/\b(\w)/\u$1/g; } elsif ( $mode == 2 ) { $str = lc($str); $str =~ s/(\w)\b/\u$1/g; } else { $str =~ s/(\w)\b/\u$1/g; } return $str; } } </code></pre> <p>See also <a href="https://stackoverflow.com/questions/1529047/why-use-if-else-if-in-c">Why use if-else if in C++?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload