Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Textual Analysis and Perl

by cyocum (Curate)
on Dec 01, 2003 at 22:33 UTC ( [id://311426]=perlmeditation: print w/replies, xml ) Need Help??

This is just a brain dump so many of the thoughts have not been completely thought through although it might be interesting for the everyone here.

In this node, An Anonymous Monk wants to know about how to analyze perl code for mantainability and usability. Take this idea but divorce it from its computer programming context. What is a computer program? It is text that instructs a computer to do things. As text, it is suceptable to textual analysis. The goal of this analysis has nothing to do with what you are instructing the computer to do but with what and how the author was thinking about their programming. This not only comes from looking closely at the variable and package names but also the overall structure of the program (i.e. OO vs. procedural, etc. etc.). For perl, this also includes trying to decern the reasons behind the use of different build-in and user defined functions. For other programming lanugages, this might be a bit harder since they are not as flexible as perl (I am thinking here of programming langauges also in the C/C++ family like java.)

I think this type of reflection upon a program could lead to some interesting insights on the thought processes that go into programming and the people who ultimately do the actual programming. In the near future, I may download some code to demonstrate how this could be done however this is a good point to start some discussion about this topic. Oh yes, I do understand that this is a tad bit of academic navel gazing.

Replies are listed 'Best First'.
Re: Textual Analysis and Perl
by rob_au (Abbot) on Dec 01, 2003 at 22:51 UTC
    There have been a couple of interesting threads in brian d foy's journal on use.perl.org over the last fews weeks relating to textual analysis of program code. This thread and the pretty pictures linked from it in particular are most interesting.

     

    perl -le "print+unpack'N',pack'B32','00000000000000000000001010010101'"

      "Interesting" pictures?!? Can you explain what they represent, then? I've looked at them for a while, but I have no clue what they are supposed to represent. So I just dismissed them.
Textual Analysis and Idiomatic Perl
by gwadej (Chaplain) on Dec 02, 2003 at 01:25 UTC

    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
      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
Re: Textual Analysis and Perl
by Roger (Parson) on Dec 02, 2003 at 01:41 UTC
    Why do people write poems while they could express the same idea in an essay? Why do people want to vary the same things they say? Why are natual languages so hard to parse? Individuality, creativity, freedom to express oneself, (and more??) are the words I could think of... Perl has elevated from a mere programming language to a form of freedom and individuality. Perl is not only a programming language, it is an art.

      Why do people write poems while they could express the same idea in an essay?

      There are three reasons ... sex, money and boredom.

      Where the third might very well be the lack of the first. Possibly caused by the lack of the second.

      Writing poetry is like programming in VB. You spend ages trying to express the simplest things in the most convoluted way possible just to please someone who doesn't care.

      Jenda
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
         -- Rick Osborne

      Edit by castaway: Closed small tag in signature

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://311426]
Approved by Itatsumaki
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-18 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found