Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Just look at it:

    msg = ' spacey string ';
    msg.replace(r'^\s+', '');
    msg.replace(r'\s+$', '');

    That’s clearly orders of magnitude easier to read than the Perl version.

    • Oh wait, it has to be this:

      msg = ' spacey string ';
      msg = msg.replace(r'^\s+', '');
      msg = msg.replace(r'\s+$', '');

      Or possibly chain the methods.

      msg = ' spacey string ';
      msg = msg.replace(r'^\s+', '').replace(r'\s+$', '');

      Perl can’t hold a candle to any of that.

      • Wow ... I've seen many complaints about lack of readability, but this has to be the dumbest one (not yours, of course, the original). Just wow.

        • BTW, the "blogger" is apparently only 15, acc. to his profile. After reading the rest of his post I figured he was a dumb kid. So he is.

          Sometimes I worry that the chorus of "Perl, in a Nutshell" will damage Perl's reputation ("it's so easy to write, but it's not always easy to read"). Then I think, if only I could be so lucky!

          • As a new Perl programmer, and (I will disclaim now) having never written any PHP, I'd like to point out that no computer language has intrinsic "readability". Do you remember your first time ploughing through "Learning Perl"'s 'A Stroll Through Perl'?

            No programming language is readable until someone invents a programming language that reads like a native language. Think Pseudo-code, and THAT is readable (or at least you'll get the idea...) However, no-one has made a REAL language like that. Yet.

            I've been to
            • I agree with much of what you say, but some quibbles:

              No programming language is readable until someone invents a programming language that reads like a native language.

              I don't accept that: I am fluent in Perl. It is nearly as a native language to me. I'd say no programming language -- or any language -- is readable to a person until they, simply, learn that language.

              I've been told by a Perl programmer that adding comments to Perl is "not the done thing" and is (apparently) unconventional. However, if there are comments it's readable. If there are none, it's difficult to read without having a good understanding and some patience.

              I wonder if perhaps you misunderstood slightly. I've never heard someone say that comments are not done if they are needed; the goal, however, is to make it so that your code is self-explanatory (to someone who knows Perl, of course). If I write this:

            • No programming language is readable until someone invents a programming language that reads like a native language.

              I'm not sure that's even possible, at least for any interesting problems. It's not that interesting to discuss the abstract relative readability of programming languages; syntax is just one particular expression of the semantics of the solution to a problem. You have to understand the problem and the semantics of a problem as well, and once you start talking along those lines, you have to

            • I would suggest starting with Code Complete 2 [] to fill in the gaps.

              After that you'll understand things like the inherent tradeoffs in commenting. It isn't as simple as "comments aren't done." And it isn't as simple as "comments make things readable." Instead it is much closer to, "comments are helpful but untrustworthy." Which is why there is a big emphasis on making your code (which can be trusted much more since errors there get noticed and fixed) read as much like comments as possible. (Much easier s
              • I do have a lot to learn, and your comments and observations about this are most welcome. I'll check out Code Complete 2 (aaah... expensing is awesome) and will soldier on with the learning.

                My original comments came from the fact I do black box testing at work, and have recently started to approach the source code. One way I find comments helpful is when they describe what the following function is supposed to do. If I just read through the function (slowly... :~) and figure it out I can see what it does. I
                • One of my favorite comments on commenting is I find the most useful comments state what remains invariant, while the code states what gets transformed. (From this post [].)

                  The explicit or implicit API presented by a function should not (lightly) be changed. Therefore it is often worthwhile to comment on that. And it is always worthwhile to pick a function name that tells you what it means. However the mechanics of how the function works internally should not generally be commented.

                  That said, there isn't an
      • Perl can’t hold a candle to any of that.

        But perl6 might... [] []

        In Python that can be written as:
        msg = msg.strip()
        • I expected that. Of course, until I read the documentation, I don’t actually have any idea about what strip does.

          • The Python version removes spaces from both sides of the text. Just create that in a sub{} in Perl and be done with it. : )
            • I know. I’m saying that if I see msg.strip() in a random piece of Python code, and I’ve never seen the documentation of strip, then I don’t know what it does, any more than I know what the Perl code does if I’ve never read the documentation.

        • In Python that can be written as:
          msg = msg.strip()
          Hey, but that's nearly impossible to write in Perl. Let me see:

          # in-place version
          sub strip { s/^\s+//; s/\s+$//; }
          # which would be used as
          • That won’t quite work. Make it

            sub strip { $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; }

            or maybe

            sub strip { s/^\s+//, s/\s+$// for $_[0] }
            • use strict;
              use warnings;

              my $string = " This is the test ";

              sub trim {
                      my $a = $_[0];
                      $a = join(" ", split(" ", $a));
                      return $a;

              my $nstring = trim($string);

              print "$string\n";
              print "$nstring\n";

              I don't think it takes every case...but it works with the "test" string.
            • The latter version can be generalized to:

              sub strip { s/^\s+//, s/\s+$// for @_ }

              However I don't like the idea of modifying arguments in place. Functions that do that violate my expectations. Instead I'd make a copy and return the copy. Like this:

              sub strip {
                  return map strip($_), @_ unless 1 == @_;

                  my $x = shift;
                  $x =~ s/^\s+//;
                  $x =~ s/\s+$//;
                  return $x;
              • The latter took me a while to read. I’m not used to recursion so casually… and it seems to me that deliberately creating opportunities to get the termination condition wrong is an unnecessary source of bugs.

                I agree though that returning a copy is better; I wrote my code that way only because was correcting the example code.

                Personally I’d write it like this:

                sub strip {
                    my @stripped = @_;
                    s/^\s+//, s/\s+$// for @stripped;
                    return @stripped;

                On a

  • September. Literally even!
  • There was another post about "readability" that I read this morning. I usually just post that it isn't Perl's fault that the code it hard to read it is the programmers fault/choice.
  • His entire blog is now gone.

    Here's what he originally posted:


    Perl vs. Python. 2 interpreted languages, whose purpose is to prevent you from writing C/C++ code. Programmers have argued for years on which is better, and many fanboys have been seen swearing at each other for no real reason. I'm here to say which is (IMHO) the better. To discover that, we must first see what those languages are meant to do for the average programmer.

    C and C++ are undoubtedly the best programming languages out there in te


      • I posted a more welcoming comment, posting equivalent code in C and pointing out that the Perl code was much easier to read, once you understood it, and that brain cycles were better spent working on the programming problem than the specifics of how to remove spaces from a string. I invited him to look into Perl some more before writing it off. I was hoping it might do some good.


  • This is

    "   a      spacey      string    "

    It's easy to quibble about readability with trivivial examples. But a "readable" language often implies "limited in power" too. So how would you solve the same problem with the added requirement that multiple spaces should be squashed into a single space? In Perl it's easy: you just have to add the additional statement:

    tr/ //s;


    s/\s+/ /g;

    (Too bad tr doesn't recognize the \s meta character class.)

    So how do