[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Shell script or Perl?



On Thu, 12 Jun 2003 21:20:08 -0500
Kirk Strauser <kirk@strauser.com> wrote:
> I'll admit that I'm not 100% consistent about that.  I tend to use parens
> when it helps to disambiguate the meaning of the code, along the lines of
> saying "2 * 2 + 3" or explicitly stating "3 + (2 * 2)", even though the
> statements are exactly equivalent without the parens.

    I was thinking more along the lines of these:

my %foo;
print "bar\n";
if (defined ($baz))

    First two could use parens, second case the defined has the parens with a
space even though it could do without it and where you've placed parents up
against the caller in other areas.  Although I will admit you seem to be
consistant in that when you call your own routines the parents abut the caller
whereas internal Perl calls you put the space.  :P

> > Secondly you cannot else an unless.
 
> Yes, you can.  I just tried this:

    Then they have recently added it as my Camel book, 2nd ed., only lists If
as valid for else.  Ah, my bad.  The 3rd edition does list else and elsif as
possible after unless.  Of course this still causes problems.

unless ($foo){
}
elsif (!$foo){
}

> I note that you pointed out my inconsistency, but not any unreadability.  :)

    No, I pointed out something which was valid as of the last time I
programmed Perl professionally.  I also pointed out that your inconsistency,
which is valid, is also possible and wildly encouraged in Perl.  See my and
print above as prime examples.

> I think of "idiomatic Perl" as constructs like:
 
>     foreach $_ ($switch)
>     {       
>         /^a$/ and do { print "a"; };
>         /^b$/ and do { print "b"; };
>         /^c$/ and do { print "c"; };
>     }

    You're also thinking that "readability" comes from "looks like C".  I find
C unreadable.  Mainly because, well, I'm not all that proficient in it.  I am
saying that readability in Perl to other people who program in Perl is a
problem.  Not people who are familiar with C, Pascal, ADA, Ruby or what have
you.  

    BTW, the above is still not that idiomatic.  C'mon, any C programmer
that's done a for loop over a set of pointers can catch that.  The far more
idiomatic would be:

foreach ($switch){
  if ($hash{$_}){
    print($hash{$_});
  }
}

    While they might catch onto foreach being the same as a for look iterating
over a set of pointers the exclusion of $_ would toss them for a loop (no pun
intended) then the check against the hash then printing out the contents would
certainly get them.  However the idioms are more in the shortcuts taken, the
presentation in the language than the method employed since using a hash has a
lookup is a very efficient way to get things done both in terms of time as
well as code.  :P

> instead of the more C-like:
 
>     if ($switch eq 'a') { print "a"; }
>     elsif ($switch eq 'b') { print "b"; }
>     elsif ($switch eq 'c') { print "c"; }
 
> A C programmer can figure out the second stanza, but the first will probably
> make him cuss at me.

    Again, readability to me isn't a matter of "Can random C jockey who looks
down on scripting languages puzzle this out."  Readability is "Can random Perl
jockey read this and understand it quickly in 2 years time."  I have yet to
see Perl code, either my own, my friends' or my coworkers' that has passed
that simple litmus test.  I'll grant that we can follow most of what we're
doing but more often than not things will get in the way and we end up
rewriting the selected code rather than even try to decipher what it is doing
and fix it.

[snippage about Python]

> I have to say that I liked it.

    The pattern I see of people who come from other languages to Python is
bucking the whole concept of enforced white-space, then moving onto having
problems with having only two generalized loop structures instead of a
half-dozen specialized loop structures.  Once ya get past those it is a nice
language and even the idiomatic Perl above is readable.

for element in switch:
    if hash.has_key(element):
        print(hash(element))

    2 years later I'll know exactly what that code does.  Even a C jockey who
looks down on scripting languages can know what that does in spite of the fact
that it is idiomatic.

> I still say that I go to lengths to make my Perl readable by others.  I've
> received emails from people who use Antonym who were happy about how easy it
> was to understand and modify, even if they weren't Perl programmers.

    As do I when I actually write Perl code.  In fact I dare say my Perl
code is closer to C than yours (more consistant C usage of parens being the
prime example, my($foo), print("bar\n"), etc).  But I don't let that cloud my
thinking in that it is actually a language I am going to endorse to others as
one that is readable and maintainable.  Not when there are alternatives which
clearly show that even at its best it isn't all that great.  :)

    And before anyone gets snippy, yeah, I know it is possible to do print
"foo" in Python.  There had to be one flaw in the language to prove it was
made by humans.  :P

-- 
         Steve C. Lamb         | I'm your priest, I'm your shrink, I'm your
       PGP Key: 8B6E99C5       | main connection to the switchboard of souls.
	                       |    -- Lenny Nero - Strange Days
-------------------------------+---------------------------------------------

Attachment: pgp3O8t8m6avO.pgp
Description: PGP signature


Reply to: