Hacker News new | past | comments | ask | show | jobs | submit login

q and qq are fantastic.

And qw too:

    # ['this', 'is', 'a', 'list']
    qw/this is a list/;



... but boy am I glad I don’t have to read stuff like that.


  ['but', 'boy', 'am', 'I', 'glad', 'I', 'don\'t', 'have', 'to', 'read', 'stuff', 'like', 'that']

  qw/but boy am I glad I don’t have to read stuff like that/
But why?


Or as it is written in mostly any other language:

    split("but boy am I glad I don't have to read stuff like that")
But I guess you saved three bytes, so that's nice.


Split works at run time, while qw// is just shorthand for array initialization syntax, so it works at compile time.


Split works at run time, but it could, in principle, evaluate at compile time when it has a constant argument.

For example, the ord() Perl function is evaluated at compile time if the argument is a constant string.

(It's not worth it because split() syntax isn't as clean as qw() for this purpose. split's first arg, if present, is the pattern to split on.)


If splitting a string is an important optimization point in your program, neither language is a good choice.

You fail to explain why other languages do not grow these vestiges. I can only assume the implicit explanation is that you believe them to be incompetently designed, whereas I believe the exact complement: it looks like a good idea, but it isn’t.


There are many good ideas that aren't in one programming language or another.

Does that actually mean they are bad ideas? No. It only means that particular language didn't copy that particular idea.

There may be bad ideas that are in many languages, that doesn't somehow make them good.

---

Quote-words are used regularly in Perl because they are clear and useful.

    for (qw' alpha beta charlie ') {
      say
    }
    for (split '', 'alpha beta charlie') {
      say
    }
    for ( 'alpha', 'beta', 'charlie' ) {
      say
    }
The `qw` emphasizes that we are dealing with `alpha`, `beta`, and 'charlie`. The other ones have extra noise that is only there to satisfy the compiler.

Now imagine you have to add `delta` to the list.

With the `qw` you only have to press the spacebar and the letters `d e l t a`. You don't have to worry if you accidently left off a `'`, because you didn't need to add one.

To be fair, the `split` would have the same benefit. But it is still more error-prone. For example I wonder how many people didn't notice that the first argument to `split` was an empty string when it should have been a string with one space in it.

I didn't even notice it, and I've been programming in Perl for decades.


Such things are called "syntactic sugar". They are common in other languages. Quote: «A construct in a language is called "syntactic sugar" if it can be removed from the language without any effect on what the language can do: functionality and expressive power will remain the same.»[0]

Another quote: «Data types with core syntactic support are said to be "sugared types." Common examples include quote-delimited strings, curly braces for object and record types, and square brackets for Arrays.»

[0]: https://en.wikipedia.org/wiki/Syntactic_sugar




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: