maddfisherman has asked for the wisdom of the Perl Monks concerning the following question:

just looked at the perl style doc and have 2 questions,
1) it says space around complex subscript ... I know what subscript is but what makes it complex?
2) it says use cuddled elses ... ???? I have no clue what that means.
thanks for your time.

Replies are listed 'Best First'.
Re: style quest
by kjherron (Pilgrim) on Aug 27, 2001 at 05:08 UTC
    A complex subscript is an array subscript (or hash key, probably) which is a complicated expression rather than a simple one:
    simple: print $day_of_week[$wday]; complex: print $day_of_week[ ((localtime($^T))[6] + 4) % 7 ];
    Cuddled elses are elses/elsifs on the same line as the closing "}" from the preceeding "if" statement:
    if ($foo) { bar; } else { baz; }
    The style guide actually recommends uncuddled elses where the else is on a separate line from the preceeding "}":
    if ($foo) { bar; } else { baz; }
Re: style quest
by clintp (Curate) on Aug 27, 2001 at 04:57 UTC
    A cuddled else looks like:
    if (FOO) { 1; } else { 1; }
    Because the else is cuddled between the {}'s.
Re: style quest
by dws (Chancellor) on Aug 27, 2001 at 07:45 UTC
    "Style" is subject to concensus. The perl style guide captures consensus within one part of the Perl community at one point in time.

    The recommendation to use space around "complex" subscripts is intended to enhace readability. It's easy for the eye to scan   return $foo[$i]; but it's harder to pick out what's going on with something like   return $foo[rand(scalar @foo)]; without adding a bit of whitespace. Many find   return $foo[ rand(scalar @foo) ]; to be more readable. The added whitespace makes it easier to visually scan the subscript.

    Vertical whitespace is a related issue. Historically, vertical whitespace was once quite precious: you only got 24 lines of it on most displays. Consensus says to use enough to make things readable, but not too much. The "cuddled else" form (so-called because the else is cuddled between two braces) use one less line than the uncuddled form. Compare

    if ( $foo ) { bar(); } else { baz(); }
    with
    if ( $foo ) { bar(); } else { baz(); }
    The cuddled else form uses one less line without (arguably) reducing readability. It's a style issue on which not all agree.

    One of the more important aspects of style is consistency. Consider what the Perl style guide has to say, and test it to see if it works for you. Then pick a practice and use it consistently. Readers can adjust to different styles, but inconsistency starts raising red flags.