Page 1 of 7 123 ... LastLast
Results 1 to 10 of 61

Thread: Why I love/hate Perl

  1. #1
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Why I love/hate Perl

    pmasiar has one on Python, I will have one on Perl (any Ruby addicts here?)

    One of the things that people complain about is Perl being unreadable. Why it can be true, it depends on the coder's habits and mindset.

    Perl is as unreadable as a terminal is unusable. Think about how you learned about various simple commands (ls, cd, man, grep, awk ...), you either read it somewhere or someone told you or you transfer some knowledge you had from somewhere else (in which case #1 or #2 probably happened).

    Here is an example of taking readable Perl code and making it unreadable:
    Code:
    open $file, "/etc/passwd" or die$!;
    while ($line = <$file>) { #read in a line from $file and store it in $line
      @record = split /:/, $line #split every line on colons and put it into record(note the array sigil)
      print "user: ";
      print $record[0]; #print the username
      print "\t\thome directory: ";
      print $record[5]; #print the home directory
      print "\n";
    }
    close $file;
    Readable, no?

    Here's the "unreadable" kind

    Code:
    open $passwd, "/etc/passwd";
    %_ = map { /^(.*?):(?:.*)+:(.*?):.*?$/gis } <$passwd>;
    map { print "user: $_\t\t\thome directory: $_{$_}\n" } keys %users;
    close $passwd;
    Now to explain it:
    Since Perl and Python can be used for functional programming (ie: Scheme, Haskel type), they have a map function which takes a block of code and applies it to a list (to every item in the list). Map sets the $_ variable to the item it is applying the block to.

    Map takes the regular expression and applies it to every line in the file (<> fetches a line from an input/file stream), then map takes the regex and applies it to the line.

    The Regex (pre syntax got their start in awk):
    ^ means beginning of the line
    $ means the end of the line
    stuff surrounded by parenthesis is to be grouped/extracted, when it is extracted, the first item gets put into $1, second into $2, and so on until $9 (perl can't hande more than 9 matches which is a limitation which I haven't met yet in practice), ?: in the beginning of parenthesis tells perl to not extract this grouping.
    period/dot by itself means any printable character
    * means 0 or more of the character
    ? means to not be greedy when matching (grab as few as possible)
    + means 1 or more of the character (a+ is same as aa*)

    the matching operation is
    STRING =~ m/PATTERN/FLAGS;

    but when you leave out the STRING (which can be a variable), it is assumed by perl that you mean $_ (which is what map sets on every iteration). Map returns the list value of every "run" (unless you force scalar context)

    When the regex matches the line, the first stuff before the colon gets put into $1 (the username) and $2 becomes the home directory.

    at the end of map, it returns a list composed of of the $1 and $2 ($1,$2,$1,$2, ...), a hash in Perl is just a key,value,key,value list (array). So perl maps the list returned by map onto a hash.

    next map simply prints the key and the hash value at that key for all keys in the hash

    an example of how map can substitute a loop:

    C-like Perl array iteration
    Code:
    @array = (1,2,3,4,5,6,7,8,9);
    for ($i = 0; $i <= $#array; $i++) { ### $#array is the index of the last set element in @array (8 in this case)
      print $array[$i] . "\n";
    }
    Perl iteration
    Code:
    @array = (1,2,3,4,5,6,7,8,9);
    for (@array) {
      print $_ . "\n";
    }
    single line version of the above
    Code:
    @array = (1,2,3,4,5,6,7,8,9);
    print $_ . "\n" for(@array);
    }
    the map version
    Code:
    @array = (1,2,3,4,5,6,7,8,9);
    map { print $_ . "\n" } @array;
    NOTE: @array is explicitly declared for clarity purposes

    NOTE2: when you are matching only 1 thing in a string to be used somewhere, usually you would use
    Code:
    $item = $1 if ($input =~ /PATTERN/);
    which is clearer than doing the "proper" if block.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  2. #2
    Join Date
    Jun 2007
    Location
    Stoughton MA
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Why I love/hate Perl

    I love Perl for smaller applications. I use it almost every day at work and it does a lot of things well. It was also my first language so it tends to be the one I am most comfortable with.

    I don't like it for large applications however, it just gets too messy. I do a lot of UPnP programming at work and on a few occasions I have had to write some large and complicated test automation tools. I used Perl because I have a decent UPnP module, but now I hate maintaining the whole thing.

    I had to learn C# at one point and used it to write a fairly large test suite for a particular service using XMLRPC and it is very neat and easy to modify. That was my first foray into true OOP. As a result, I have moved on to C++ and Python for the bigger stuff. Perl's OO implementation is kind of a hack IMHO.

  3. #3
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Why I love/hate Perl

    I wish I new Perl better, but I see no reason to learn it for anything other than the experience (I like to learn). I also have the same feeling on Ruby...

  4. #4
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Why I love/hate Perl

    Quote Originally Posted by scruff View Post
    I love Perl for smaller applications. I use it almost every day at work and it does a lot of things well. It was also my first language so it tends to be the one I am most comfortable with.

    I don't like it for large applications however, it just gets too messy. I do a lot of UPnP programming at work and on a few occasions I have had to write some large and complicated test automation tools. I used Perl because I have a decent UPnP module, but now I hate maintaining the whole thing.

    I had to learn C# at one point and used it to write a fairly large test suite for a particular service using XMLRPC and it is very neat and easy to modify. That was my first foray into true OOP. As a result, I have moved on to C++ and Python for the bigger stuff. Perl's OO implementation is kind of a hack IMHO.
    Perl OO IS a hack, without any IMHOs ... everyone knows that. Although Perl6 is supposed to address this and follow Python in the everything is an object mentality.

    One of the reasons I like Perl is that it also has ways to cut down on the amount of code you need to write (like using a regex on a file and then sticking the whole thing into a hash).
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  5. #5
    Join Date
    Jun 2007
    Location
    Stoughton MA
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Why I love/hate Perl

    Well that would be nice. I haven't looked into whats coming up in Perl 6. Off to do that now

  6. #6
    Join Date
    Jan 2005
    Location
    India
    Beans
    385
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Why I love/hate Perl

    I learnt Perl once. Even wrote a simple script that took several pictures from a directory and built a kind of album out of them. That was in January 2007. Now I remember nothing of it.

    I'd really like to learn Perl because it's nice the way you can make your programs really short and succint. But the language itself makes it harder for me to really get into the depth of it and learn it "inside out" like you can learn a language like C.

    Someday, I'll get a real book and get down to learning Perl. I've had my eyes on Perl 6, but I doubt it will ever be released.
    I am the turnip. No, not a turnip, the turnip. Visit my blog here.

    I need help with my Code Dojo. If you're interested, please drop me a line.

  7. #7
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Why I love/hate Perl

    get the source to perl, then you will know how Perl works

    as for script to make album, remember the algorithm ...

    Code:
    get a list of files
    foreach $i (file) {
      generate a page for file[$i](write out HTML code or whatever)
      if ($i != $#file_list) {
        create a link to next file
      }
    }
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  8. #8
    Join Date
    Aug 2007
    Location
    Kansas City, MO
    Beans
    Hidden!
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Why I love/hate Perl

    The beauty of Perl, without getting into which language is better (Perl verus Python versus Ruby, etc...), is that it is a standard package on default installs of all major OSes. As much as one might love Python and Ruby, in a standard enterprise environment, without gaining an exception, you will not find Ruby and/or Python installed by default, whereas you will find Perl. I love Linux and have tried a vast majority of the distros, but sadly Linux is still not the defacto standard operating system for major eCommerce applications for major companies. This is not to say you will not find some that are running it as their standard, but working for a Fortune 500 company I can say that we are still more of a Sun or IBM shop then we are RHEL or any other Linux distro.

    I've written some bigger applications in Perl but without good session management builtin it becomes rather tedious. Its great for manipulation and quick and dirty scripts, without a doubt.

  9. #9
    Join Date
    Aug 2005
    Beans
    182
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Why I love/hate Perl

    Quote Originally Posted by KCPokes View Post
    The beauty of Perl, without getting into which language is better (Perl verus Python versus Ruby, etc...), is that it is a standard package on default installs of all major OSes. As much as one might love Python and Ruby, in a standard enterprise environment, without gaining an exception, you will not find Ruby and/or Python installed by default, whereas you will find Perl. I love Linux and have tried a vast majority of the distros, but sadly Linux is still not the defacto standard operating system for major eCommerce applications for major companies. This is not to say you will not find some that are running it as their standard, but working for a Fortune 500 company I can say that we are still more of a Sun or IBM shop then we are RHEL or any other Linux distro.

    I've written some bigger applications in Perl but without good session management builtin it becomes rather tedious. Its great for manipulation and quick and dirty scripts, without a doubt.
    A very good argument. I've tried Perl. I've tried Python. Python is easier to write for me as a beginner, but Perl is more available. Really want to understand them both but my time is limited.
    Linux User #395848
    Blog: http://digitalnotions.net

  10. #10
    Join Date
    Oct 2005
    Location
    Davao, Philippines
    Beans
    4,830

    Re: Why I love/hate Perl

    i love Perl because its opengl bindings is as fast as C , I hate it because after purchasing three books a couple of years ago from begginer ,Reference, Advance . I'm still having difficulty reading individual perls scripts. so when i found python, i just settled with it.

Page 1 of 7 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •