Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl is more intuitive

by kiat (Vicar)
on Aug 18, 2005 at 16:27 UTC ( #484858=perlmeditation: print w/ replies, xml ) Need Help??

Hi monks,

Maybe I'm used to Perl in respect to $ for scalars, @ for arrays and % for hashes.

I'm reading up on PHP and I come across code such as:

while($element = each ($fruit)) { echo $element['key']; echo '-'; echo $element['value']; echo '<br />'; }
which is equivalent to Perl's

while (($key, $value) = each (%fruit) { print $key; print '-'; priint $value; print '<br />'; }
When I read Perl code, the % immediately tells me that I'm looking an associative array. In contrast, all variables in PHP are prefixed with $, which I find rather unhelpful.

If I'm not wrong, Ruby has none of those and I imagine that it must be a lot more cryptic, because the variables can read like function words:

for x in 0...c.length do print c[x], " " end
Which are the variables and which are the function words?

What are your views?

Comment on Perl is more intuitive
Select or Download Code
Re: Perl is more intuitive
by Ven'Tatsu (Deacon) on Aug 18, 2005 at 17:16 UTC
    I also like Perl's use of sigils to identify access type. Though I think I will like Perl 6's use as identifying variable type more.
    I've often though that the use of Hungarian notation in C/C++/Java and similar is a result of sigil envy.
Re: Perl is more intuitive
by zshzn (Hermit) on Aug 18, 2005 at 17:29 UTC
    Although the Ruby example may look cryptic at first, it is easily decyphered. To someone that is familiar with Ruby syntax and functions, the variables would be obvious. The prefix in Perl is particularly helpful in knowing what are variables and what aren't, although if they are named clearly they would be noticed as variables regardless of the prefix. PHP's method could be more confusing for a Perl programmer, to whom it would seem odd that hashes and arrays aren't prefixed properly, over a C programmer, who would just see it as all variables get a prefix. Personally, I like how Perl does it, but that could just be because I use Perl.
Re: Perl is more intuitive
by samizdat (Vicar) on Aug 18, 2005 at 19:26 UTC
    A cow-orker and I were just ruminating over this, and I made the point that I wish that Ruby references were typed differently for hashes and arrays, as in a{x}[y]. One could sort of argue that arrays are just a special case of hashes, but that leaves out the ordinality and indexability of arrays which is not possible for hashes. They only overlap in some ways.

    Perl's typing does help here, but I have to say that the rest of Perl's reference syntax is abominable, at least for multi-level references to structures, and Ruby's clarity here far outweighs Perl's typing by first character indicator. When you reference an array or hash element, you're going to make it clear enough by using either a numeric symbol or a string as the key / index which you're accessing. It's more problematic when it's a variable in the brackets, but if you don't know what your variables are doing at night, you're already really in the dark. :D

      Ah, but the overlap is a feature. Not to mention that by adding a few methods ([], []=, etc.) you can make objects look Array- or Hash-like. As with tie in Perl there's places it makes sense and places it doesn't, of course. See the chapter on "Duck Typing" in the second edition Pickaxe for more discussion.

      --
      We're looking for people in ATL

        Haven't even gotten to PA#2 yet, 1.6 and PA#1 are so @ss-kicking that I'm grokking hard. Helps that I used to be a heavy Smalltalk user. Subclassing Arrays and Hashes and Strings comes naturally, though I miss the groovy IDE.
Re: Perl is more intuitive
by sauoq (Abbot) on Aug 19, 2005 at 02:39 UTC
    What are your views?

    PHP isn't Perl.

    For Perl programmers, its apparent similarities to Perl tend to be more frustrating than helpful because they are only cosmetic similarities. Under the hood, the languages are very different from each other.

    The example you give is usually the first that Perlers faced with learning PHP run into. It is also usually the first to be forgotten once familiarity starts to settle in. One that I banged my head on for a while was that Perl's concept of context doesn't translate into PHP. For instance, I had a PHP function defined to take multiple arguments and then spent far too long trying to figure out why I couldn't shove all those arguments into an array and call myfunc($array)... duh.

    I think most experienced Perl programmers who are introduced to PHP for the first time tend to view PHP as an inferior if not downright dysfunctional Perl¹. It isn't. It's actually a decent language, designed primarily for a limited domain, that meets its requirements fairly well. But, PHP isn't Perl. If you keep that in mind while you learn it, you might have an easier time of it.

    1. Well, I did anyway.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Perl is more intuitive
by spiritway (Vicar) on Aug 19, 2005 at 04:55 UTC

    When I first came to Perl, I was annoyed by the sigils because I would always forget to use them when naming variables. However, I quickly came to understand how very useful it is to be able to identify the sort of value you're looking at in a glance. The only similar method is the so-called Hungarian notation, which is unwieldy and ugly (IMNSHO, anyway). Now I wonder how I ever got along without the sigils. They make coding and debugging so much simpler.

Re: Perl is more intuitive
by Joost (Canon) on Aug 19, 2005 at 11:13 UTC
    The ruby code you've written doesn't work for generic hashes (it does for arrays but it's not at all idiomatic).

    You can write the ruby code for hashes a lot more intuitively like so:

    #!/usr/bin/ruby # example data hash = { 'a' => '1', 'b' => '2', 'c' => '3', } hash.each do | k, v | puts "#{k} - #{v}<br/>" end

    Update for the interested: ruby does have sigils, but they denote "scope" instead of "type": $ - global, @ - instance property and @@ - "static" class property. No sigil basically means lexical scope (automatic variable).

    The reasoning behind this probably is, that variables that can be declared "somewhere else" in the code have sigils, but "local" variables don't need sigils since you can see that they are variables because they're declared a close by. In some respects ruby is even more fanatic about saving characters than perl :-) That there is no way to distinguish "type" by sigil is a good thing - since it makes it a lot easier to emulate the built-in types' API with user-defined classes.

      That looks very clean. And I supposed the each is a good-enough contextual clue that a hash is being called.
        each is actually a method name that's used for many of the container types; arrays use it too:

        arr.each { | x | # iterates the elements, NOT the index puts x }

        Basically, each here is just a method that recieves a block (like perl's anonymous subroutines) as an argument. The container type is then responsible for calling the block for each element.

        Any class can provide it's own each method - this is not a built-in function like perl's foreach.

Re: Perl is more intuitive
by Anonymous Monk on Aug 19, 2005 at 12:28 UTC
    Most languages don't use sigils for their variables, and they aren't massively abandoned because they are 'cryptic'. Python and Java are often considered to be less cryptic language than Perl - which is often seen as quite cryptic, or even shell, which can be quite cryptic as well.
    Which are the variables and which are the function words?
    You know, if context cannot tell, and if you are only looking through a peephole (that is, seeing a few lines of code, and not a declaration of the variable or function), it usually doesn't matter. If one sees:
    print foo;
    in a language where parenthesis aren't required after a function call, it really doesn't matter whether foo is a variable, or a function. It's printing the value of foo.

    In fact, understanding programming in general becomes a lot easier if you view scalars, arrays and hashes as functions. A scalar is just a function that returns a fixed value (although you can later change what the value is). An array is just a function that takes an integer as argument, while a hash is a function that takes a string as argument.

    I find sigils handy because they allow me to do interpolation. But for me, that's their only use. It doesn't make programs more cryptic or less cryptic.

      I'm not sure if this related but imagine nouns in English weren't marked for plurality:

      one child->many child
      one apple->two apple

      1) The apple are good.
      2) The apple is good.
      3) Many apple are good.
      4) One apple is good.

      You then probably have to use quantifiers (one, many) or verbs (is, are) to indicate whether you mean a singular instance of the item or many instances.

      Additional note:

      OTOH, maybe there's some redundancy there. Because the verbs already tell you whether you're talking about a singular entity or a plural one (a case in point: The sheep is still alive vs The sheep are still alive.).

      In Chinese, nouns themselves are not marked for plurality. Instead, quantifiers are used to do the job.

        Oblig. The Simpsons reference: "We all know Pi(R)**2, but today, pie are justice: I welcome it." (Frederic Tatem)

        <-radiant.matrix->
        Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
        The Code that can be seen is not the true Code
        "In any sufficiently large group of people, most are idiots" - Kaa's Law

      Python and Java are often considered to be less cryptic language than Perl - which is often seen as quite cryptic.

      I've always wondered about the "more cryptic" vs. "less cryptic" comments. There are clearly cryptic languages, like this little nuisance, but the rest really seems more a matter of whether or not a language is more or less compatible with an individual's way of thinking.

      For example, I have good friends that are Python programmers and cannot grok Perl. To quote them, "Perl is too disorganized for me: Python works like I think." I, on the other hand, can't stand Python{1} (all that significant whitespace, the lack of sigils, and not using braces for blocks all scare me) because Perl works exactly how I think.

      That doesn't mean one is "more cryptic" than the other; rather, it just means that different programmers find different approaches and syntax to be intuitive.

      1: I still use Python, and it does have some nifty things I wish were built into Perl, like the set() function(s).

      <-radiant.matrix->
      Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
      The Code that can be seen is not the true Code
      "In any sufficiently large group of people, most are idiots" - Kaa's Law

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-09-22 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (193 votes), past polls