Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

What did you have to Unlearn

by seeker (Curate)
on Jun 08, 2001 at 20:50 UTC ( [id://86974] : perlmeditation . print w/replies, xml ) Need Help??

Looking at an earlier post on what it takes to learn Perl I began to wonder about some of us old timers who came to Perl after using other languages. What did we have to unlearn to learn Perl?

It many not be anything big. For me it was getting used to variable names with special characters in front, and using those variables inside of quotation marks.

How about some other old timers? What did you have to unlearn?

Replies are listed 'Best First'.
Re: What did you have to Unlearn
by dws (Chancellor) on Jun 08, 2001 at 21:16 UTC
    I had to unlearn a pattern of thinking that put conditionals first. Code like
    if ( $DEBUG ) { print "debug info\n"; }
    use to fly off of my fingertips without much thought. It took a while to try writing   print "debug info\n" if $DEBUG; Once I saw that this could lead to less cluttered, easier-to-read code, the change was easy. Using "unless" took a while longer, since I balked at the built-in negation. I use it sparingly, for things like   unlink $file or die "$file: $!" unless $DEBUG;

Re: What did you have to Unlearn
by marcink (Monk) on Jun 08, 2001 at 21:09 UTC
    I don't remember UNlearning anything, but there are some things about perl that took me some time to get used to them. One such thing is definitely the context -- I believe I have never previously seen a language in which the value returned by an expression depends on what you want to do with it. It's the most un-C-like feature in perl (although it's somewhat similar to functional languages' type-guessing).

    Another thing is perl's idea of OO. It's strange. It's funny. It's not actually OO by most definitions of the term -- it does not even pretend to protect object's variables, for example. But it works ;-)

    Last but not least there's the matter of map and grep -- for a very long time I used loops and (god forbid) indexes to convert one array into another. foreach and map are just beautiful.

    The interesting part is that you don't *have* to learn these things to start programming in perl -- sooner or later they will get to you anyway ;-)

      Another thing is perl's idea of OO. It's strange. It's funny.

      You think Perl's OO is funny, strange?? Try Cobol's OO... It's plain kinky ! :)

      ... Quidquid perl dictum sit, altum viditur.
Re: What did you have to Unlearn
by danger (Priest) on Jun 08, 2001 at 22:17 UTC

    Not "unlearning" per se, but just learning, and running into problems with previous habits. Lots of things --- and in both directions. Like you, it took me a little while before putting symbols in front of variables was a habit. Of couse, now I can usually count on throwing in a few @'s and $'s when I'm coding in C.

    It also took me a while to really utilize the foreach loop rather than the C-style for(;;) loop --- however, I also learned early *not* to use foreach (1 .. 1000000), because it built the whole list in memory. That was optimized to a counting loop (in 5.005). It was rather easy to learn not to do it because there was a penalty involved. Coming back to the foreach version took longer because there is no real penalty for sticking with the C version. Well, actually there is a minor penalty --- a suprising number of people will prejudge you to somehow be a less competant Perl programmer if you use the C-style loop rather than the more "idiomatic" foreach version (even though it wasn't all that long ago (prior to mid 1998 or so) when it wasn't always a good idea).

    On the whole, picking up Perl with all it's idiosyncracies, funky symbols, context sensitivity, statement modifiers and such wasn't noticeably a problem. It's only when you need to go back to another language that you *really* notice what's missing and have to remember/relearn how to accomplish even relatively simple tasks :-)

Re: What did you have to Unlearn
by Abigail (Deacon) on Jun 09, 2001 at 03:15 UTC
    I had to unlearn OO. I've been programming Perl for more than half a dozen years, and I still get upset than Perl only allows for one instance variable per object, degenerating OO to the horror Perl calls OO.

    Almost anything else in Perl came from something familiar to me, most notably C, sh, awk, and sed.

    -- Abigail

Re: What did you have to Unlearn
by mpolo (Chaplain) on Jun 08, 2001 at 21:13 UTC
    I'm still unlearning... Some highlights:
    • Strings are not arrays of characters.
    • The = is assignment, not comparison.
    • String comparison differs from number comparison.
Re: What did you have to Unlearn
by lemming (Priest) on Jun 08, 2001 at 21:11 UTC

    I wouldn't say I've unlearned anything. (Maybe forgotten a few things...)
    I still use algorithms that were useful in C and Info/Basic, but some of the statements are rearranged. The biggest difference is just the way for(each) loops are done.
    The second is how to avoid using ! and using unless instead of if in appropriate situations.

    The only things to unlearn are bad practices. Good coding shouldn't have to be unlearned.

Re: What did you have to Unlearn
by Sifmole (Chaplain) on Jun 08, 2001 at 21:03 UTC
    I had to "unlearn" indexing into arrays. for, foreach, and map are your friends.
Re: What did you have to Unlearn
by andreychek (Parson) on Jun 09, 2001 at 00:12 UTC
    I agree with what several others have said -- it hasn't been too much unlearning for me, as much as it has learning a new way of doing things.

    If I had to say I unlearned something, it would be my in the box method of thinking with a programming language. In many languages, there is one way to do things, and it isn't necessarily very creative. With Perl, I find I need to step back a bit more and think about the possible methods one could use to accomplish the tasks at hand.

  • What functions does perl provide to help me accomplish this task?
  • Has anyone else tackled this problem previously? If so, go grab it from CPAN :-)

    And after I have code written, I don't know how many times I end up reviewing it, wondering if there might be a "better" way of going about the current task.
Re: What did you have to Unlearn
by mr.nick (Chaplain) on Jun 08, 2001 at 22:42 UTC
    Not that it was difficult, but I had to force myself NOT to worry about memory management and garbage collection.

    Working with complex data structures (heaps, arrays, lists, btrees, hashes) in C required SO MUCH overhead; in Perl: nada.

    It was hard at first not worrying about it :)

Re: What did you have to Unlearn
by Cybercosis (Monk) on Jun 08, 2001 at 22:45 UTC
    The point isn't so much to unlearn anything as it is to learn new ways of doing the same old things. One example that has been given is using map() instead of blithely iterating over an array. I personally had to get used to using postfix conditionals (XYZ if(foo);) for one-statement conditional statements instead of just sticking the statement to be executed on the next line.
    As a matter of fact, PERL helped me understand some of the finer points (no pun intended, it'll make sense later) of other languages. It wasn't until I grokked PERL's references that I really understood the power of C's pointers (makes sense now, doesn't it? *ducks flying objects*).


    nemo accipere quod non merere

Re: What did you have to Unlearn
by Beatnik (Parson) on Jun 08, 2001 at 21:09 UTC
    I wouldn't say I unlearned. I think it's more like mixing up different languages, if we like it or not (using Perl logics in Cobol code is just plain wicked :)) ). I actively program in 2 other languages besides Perl (most other languages I learned once are still in my memory somewhere, somehow). If you don't practise something (anything!), you loose your touch. It's all about setting priorities :)

    ... Quidquid perl dictum sit, altum viditur.
Re: What did you have to Unlearn
by John M. Dlugosz (Monsignor) on Jun 09, 2001 at 01:47 UTC
    I didn't have any trouble with it, but I did notice a fundimental difference between Perl and C++. In C++ returning a ref/pointer to a local variable is a bad thing. In Perl, it is a useful idiom.

    As for putting things with meaning inside quotes, I got that shock when I first learned C, and saw \n in the text. It was novel that something inside quotes was not sacred but manipulated.


Re: What did you have to Unlearn
by spaz (Pilgrim) on Jun 08, 2001 at 22:12 UTC
    I had to unlearn that I can't program in English.

    $ftp->mkdir( $dir ) and $ftp->cd( $dir ) unless $ftp->cd( $dir );

    -- Dave
      Shouldn't that be something like...
      $ftp->cd($dir) or ($ftp->mkdir($dir) and $ftp->cd($dir));
      Technically, the most english way of writing the code (that actually works) is:
      $ftp->mkdir($dir) and $ftp->cd($dir)
      If you don't mind making the standard case having one extra file system access and the non-standard case having one less access.

        Try it! The first time you end up in the dir. The second time not. Because mkdir fails. Because the dir is already there. Ok, there are Server around, which go "Make a Dir? Is the dir there or not? So don't bother me with particulars!". There your tactic succeeds.