🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #4356

Re: Readable Perl (was: Re: Relative Speed of Perl vs. Tcl vs. C)

#4356
From: Tom Christiansen
Date: Fri, 16 Feb 1996 22:48
125 lines
4462 bytes
 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc, jared@valleynet.com (Jared Still) writes:
:merlyn@stonehenge.com (Randal L. Schwartz) wrote:
:
:>>>>>> "John" == John Viega <jtv2j@mamba.cs.virginia.edu> writes:
:
:>John> People bitch about the readability of Perl non-stop.  In fact, I have
:
:>OK, now which do you find most readable...
:
:>      $i = 1;
:>      while ($i <= 10) {
:>              print "I can count to $i\n";
:>              $i++;
:>      }
:
:.....
:
:>      foreach $i (1..10) {
:>              print "I can count to $i\n";
:>      }
:
:>Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
:
:None of the above.
:
:I like my curly braces to line up.
:
:       foreach $i (1..10) 
:       {
:               print "I can count to $i\n";
:       }

CBD -- C brain damage.  While I understand why you are doing this, it
doesn't make any sense in Perl, only in C.  You are working under the
misconception that the "while (...)" etc is the "ok, let's is the begin
while loop now" construct.  It's not.  That construct is "while (...) {".
Note the mandatory brace.  In pseudo code, you'd have

    1 iterate across i from 1 to 10
    2    print something
    3 end i iteration

Which directly translates 

    1 foreach $i (1..10) {
    2   print "I can count to $i\n";
    3 }

Statements 1 and 3 are the begin and end.  The way you do it, you've got a
dangling curly that takes up a line without doing anything, and confusing
readers about what it's there for.   The close curly syntactially matches
up with the if, while, for, or whatever, because the open curly is a part
of them, not optional as it is in C.  To find where the block that
line 3 closes starts, just find the first non white space at that 
same column.

That's why Larry writes in perlstyle(1)

    Regarding aesthetics of code lay out, about the only thing Larry cares
    strongly about is that the closing curly brace of a multi-line BLOCK
    should line up with the keyword that started the construct. Beyond
    that, he has other preferences that aren't so strong:

    *   4-column indent.
	^^^^^^^^

    *   Opening curly on same line as keyword, if possible, otherwise line up.
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                               ^^^^^^^^^^^

    *   Space before the opening curly of a multiline BLOCK.

    *   One-line BLOCK may be put on one line, including curlies.

    ......


The "when it's not possible case" is demonstrated here:

    while (<>) { # YANETUT
        s/^(([A-Z].*?)(:.*?)*?)(:\s*)?\n// 
            and ($3 || $4 ? $card : $sect) = $1;
        next if /^\s*[=-]/;
        for (split /\n(?=  \S|\+)/) {
            s/\s+/ /g; s/^ //;
            $new = s/^\+\s+// ? '+' : '';
            next if $opt_n and !$new;
            s/([a-z]\.) ([A-Z])/$1\n$2/g if $opt_s;
            if ( ($opt_e ? $card : $_) =~ /$query/o || 
                    (!$opt_e && $opt_h && $card =~ /$query/o)) 
>>>>        {
                s/\n/ /g if $opt_s;
                $matches++;
                write;
                ($psect, $pcard) = ($sect, $card);
            }
        } 
    } 

You only do that when you've got continuation lines.

    if (    asdfasdfsda
	 && asfljkasdjkasdf1
	 && asfljkasdjkasdf2
	 && asfljkasdjkasdf3
	 && asfljkasdjkasdf4
       )
    {
	zmcbzxb
    } 

But normally just 

    if (asdfasdfsda) {
	zmcbzxb
    }

--tom
-- 
Tom Christiansen      Perl Consultant, Gamer, Hiker      tchrist@mox.perl.com

    X-Windows: Don't get frustrated without it.
	--Jamie Zawinski
Technical Details
Message-ID: <4g31jn$q71@csnews.cs.colorado.edu>
Path: arc1-ca.pugleaf.net!in1.uu.net!csnews!mox!tchrist
References: <4e3a2u$eoa@wcap.centerline.com> <JTV2J.96Feb12142743@mamba.cs.virginia.edu> <ukd97hwzkc.fsf_-_@linda.teleport.com> <4g23sv$dht@alpine.valleynet.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!csnews!mox!tchrist
From: Tom Christiansen <tchrist@mox.perl.com>
Newsgroups: comp.lang.misc,comp.lang.perl.misc,comp.lang.tcl,comp.lang.c,comp.lang.java
Subject: Re: Readable Perl (was: Re: Relative Speed of Perl vs. Tcl vs. C)
Date: 16 Feb 1996 22:48:23 GMT
Organization: Perl Consulting and Training
Message-ID: <4g31jn$q71@csnews.cs.colorado.edu>
References: <4e3a2u$eoa@wcap.centerline.com> <JTV2J.96Feb12142743@mamba.cs.virginia.edu> <ukd97hwzkc.fsf_-_@linda.teleport.com> <4g23sv$dht@alpine.valleynet.com>
Reply-To: tchrist@mox.perl.com (Tom Christiansen)
NNTP-Posting-Host: perl.com
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Originator: tchrist@mox