in reply to Textual Analysis and Perl

I've been thinking about this concept for a long time. Especially after reading some of Larry Wall's writings on Perl and natural languages.

I've been trying to advocate the idea of various programming language idioms being useful in what they convey to the maintenance programmer about intent. Like natural language idioms, programming idioms say more than the sum of their syntax.

I think if we began to consciously explore the use of the different idioms, we could more accurately understand the intent of the original programmer. One other important point is that like natural language idioms, programming idioms are cultural. If you are new to the culture, the idioms are strange and require effort to understand. Once you are comfortable with them, more information is conveyed than appears in just the syntax.

For example, I like to use the argument of conditional logic in Perl to try and illustrate these principles:

# form 1 if(condition) { action; } # form 2 action if condition; # form 3 condition and action;

(Update: thanks Zaxo for correcting my and/or confusion.)>/p>

All of these structures cause the computer to execute very similar code. (Let's ignore the actual opcodes and return values for the moment. Let's also ignore the other similar constructs for now.) I maintain that these three different forms place different levels of importance on the action and condition. This is in spite of the fact that condition is always executed first and if it is true the action is executed.

To me, the first form says that the condition and action are both very important to the function of this code. The second form is kind of a guard case. The condition enables the action, but it is less important. The third form seems to place more emphasis on the condition.

It seems to me that most of the code I have read uses these idioms in the way I've described. I don't know if this is written down somewhere and I've just missed it, if it's an unconscious meme that many Perl programmers apply, or maybe I'm just dense and this is obvious to everyone else.

It seems that some level of textual analysis could discover useful idioms of this type.

G. Wade

Replies are listed 'Best First'.
Re: Textual Analysis and Idiomatic Perl
by xenchu (Friar) on Dec 02, 2003 at 02:46 UTC
    If you intend to discuss programming idiom as cultural phenomenom you will need to define 'cultural'.

    For instance, does the programmer's choice of idiom depend on what s/he was taught in a programming class. S/he may learn to use one idiom in a classroom, someone else may pick another teaching him/herself Perl. And a third programmer might learn to use an idiom because s/he saw it's use on Perl Monks. Which is a personal choice and which is 'cultural'? Or are all three 'cultural?

    I am not trying to nitpick what has been said. I am just trying to point out that this is a difficult and complex subject. A good analysis will take a lot of work by a lot of monks.

    xenchu

    Perl has one Great Advantage and one Great Disadvantage:

    It is very easy to write a complex and powerful program in three lines of code.

    ********************************************

    The Needs of the World and my Talents run parallel to infinity.

      You've hit the nail right on the thumb as they say.

      In this case I was speaking of Perl culture, although all of the others apply.

      I have dealt with many programmers who continue to write code in every language they encounter as if they were still using the first language they learned. ("You can write FORTRAN in any language.")

      I'm sure we have all seen people who stick to an approach they were taught in a programming class, whether it is correct or even makes sense in the current context, because that is how they were taught.

      In this case, I'm speaking of the shared culture of Perl (assuming it exists <grin/>). Obviously, some of the idioms above do not apply to other languages. Most languages have their own cultures and idioms. Some of those idioms translate to other languages, some don't. One of the things I like about Perl is the richness of the idioms it supports.

      Strangely, some languages seem to try to remove idioms in favor of having only one way to solve any given problem.<shrug/>.

      G. Wade
        Strangely, some languages seem to try to remove idioms in favor of having only one way to solve any given problem.

        Even worse, some people designate many of perl's unique, concise and powerful idioms as "tricks", and unilaterally declare them as deprecated. The usual rational for this is that the (mythical) "maintainance programmer" may be confused by them -- it's never 'them' that is confused, always the maintanance programmer.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Hooray!
        Wanted!