Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Unusual coding arrangements in Perl

by Win (Novice)
on May 03, 2007 at 12:19 UTC ( [id://613363]=perlmeditation: print w/replies, xml ) Need Help??

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Unusual coding arrangements in Perl
by GrandFather (Saint) on May 03, 2007 at 12:29 UTC

    Slurp all the "lines" from the (probably special) file handle "DATA" into the directories_time_tagged array, then remove the line separator sequence from each entry in the array.

    Compact? Sure. Unusual in Perl? Nope. Hard to understand described in English? I don't think so.

    Of course the description has to allow a little slop due to the influence of things such as the $/ special variable - but that doesn't really detract from the understanding does it?


    DWIM is Perl's answer to Gödel
    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Unusual coding arrangements in Perl
by demerphq (Chancellor) on May 03, 2007 at 13:04 UTC

    I dont see why this would be difficult to express in a natural language. The main issue is learning to parse it in the right order. You have to look at the innermost expressions and work outward, but that should be familiar to anybody who has done high school math and understands the concept of order of operations (taught to me as BEDMAS).

    So if you parse it in the right order it is, 'declare an array named directories_time_taged and initialize it with the contents of the DATA filehandle, and then remove the line ending from each element in the array'.

    Personally I think such brevity of expression is quite nice. Off topic, this brings to mind modern prose versus prose from a hundred or two hundred years ago, the language is the same but the usage is totally different. Older English prose seems to me to be much more verbose than modern, and while often containing a pretty turn of phrase is to me sometimes unreadable for the excessive verbiage. This is similar to various programming languages versus Perl (or similar languages like Python or Ruby). The useful information density per character is much higher in Perl, enabling complex operations to be described in a succinct and easy to understand fashion. Instead of paging through screens full of code you can look at a few lines and know whats up. Of course sometime people go too far with it and make it unreably dense, but thats usually just for fun.

    ---
    $world=~s/war/peace/g

Re: Unusual coding arrangements in Perl
by marto (Cardinal) on May 03, 2007 at 12:32 UTC
    I am not sure I see what your point is. Are you just asking for someone to document this line for you? Anything can be well documented provided that the person doing the coding/documenting understands what they have done and how to articulate that in English. For example are you telling us that this is a line of code in one of your apps, and you don't know what it is doing, or don't know how to articulate what this line is doing in such a way to make it understandable by others?

    I have seen some strange comments in 'legacy' apps/scripts written by people who long since left the respective companies. Comments along the lines of:

    /* Found this on google */ or 'I have no idea how this works!

    So cut and paste coding often results in poor documentation were the guilty party does not/can not understand and document what a piece of code does.

    Update: Does this relate your request for someone to comment code they just gave you, for the benifit of 'the common man' as you say?

    Martin
Re: Unusual coding arrangements in Perl
by Old_Gray_Bear (Bishop) on May 03, 2007 at 15:53 UTC
    The fact that a language construct can be "difficult to put into English" is a function of the power of the abstraction represented by the Language. A line of 'code' can easily represent several paragraphs of English text. This is not a difficulty, it is a consequence of the power of the tool you are using. Consider Mathematics (Group Theory), Physics (Maxwell's Equations), etc for other for other fields where a few lines of 'code' translate into many, many lines of English text.

    If you think Perl is terse, consider APL or Forth. I have seen an entire Gaussian matrix reduction expressed as a dozen *characters* in APL. (The corresponding FORTRAN program ran to over 200 lines.)

    ----
    I Go Back to Sleep, Now.

    OGB

      A line of 'code' can easily represent several paragraphs of English text. This is not a difficulty, it is a consequence of the power of the tool you are using.

      Slightly OT, but this is exactly what I thought when I was reading an electoral law regarding the distribution of seats in congress. It takes several paragraphs of confusing prose to explain something that would take a few simple equations or lines of pseudo-code!

        For my favorite example of this, see the large paragraph on my homenode headed "A Halton Borough Council public notice", and then imagine how much simpler to follow a line on this would be.

        One thing that would really enhance discussion of some things here at PM is the provision of a whiteboard. I understand the storage implications make that impractical and the risk of people posting obscenities more so, but sometimes a picture really does save a thousand words, and off-site links have a habit of going away.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Unusual coding arrangements in Perl
by talexb (Chancellor) on May 03, 2007 at 14:06 UTC

    Here's something from my recent web application:

    my %immediate = map { m/(\d+)/; $1 => 1; } grep /m_immediate_/, (keys %{$cgi->Vars});

    I have a bunch of check box controls in an HTML form, and this just gives me a hash containing the ones that are set. You have to read it from right to left .. first I get all of the keys from CGI's Vars hash, and keep only the ones containing 'm_immediate'; I collect the numeric part, make a hash element using the value, and put that into the 'immediate' hash.

    Terse, powerful, simple. That's Perl.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: Unusual coding arrangements in Perl
by naikonta (Curate) on May 03, 2007 at 16:09 UTC
    Then get yourself used to it.

    Basically, it's just an ordinary array assignment and a function is applied to that array. Anyone understands what chomp does would find extra comments as superflous, unless you mean something else:

    # From The Collaborative International Dictionary of English v.0.48 : # Chomp \Chomp\, v. i. # To chew loudly and greedily; to champ. [Prov. Eng. & Colloq. U. S +.] --Halliwell. # [1913 Webster] # # From WordNet (r) 2.0 : # chomp # n : the act of gripping or chewing off with the teeth and jaws # [syn: bite] # v : chew noisily; "The boy chomped his sandwich" [syn: champ] chomp(my @directories_time_tagged = <DATA>);

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-29 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found