Before I try to explain why the tilde (~) makes sense, let me first explain what happened to the plus (+).

Infix (in between) plus still adds two numbers: 4 + 5 will stay 9. I think everyone will be very pleased with this.

Prefix (in front) plus changed a little. It used to be a no-op, but in Perl 6 it forces numeric context. In other words, +$foo will evaluate $foo as a number, regardless of what it actually is. This instantly fixes all those ugly occurrences of $foo + 0.

Then, there is the plus in a set of bit operators. As you may or may not know already, the bit operators have all changed. This is done because they've found much better uses for the beatiful characters like | and &. In Perl 5, when you had $foo | $bar, it would do a bitwise OR. But it didn't end there: there are stringy bitwise OR and numeric bitwise OR. The operation chosen would depend on the values of the two variables. This is highly unperlish, because in most other situations we have different operators for strings and numbers, because they just aren't the same thing.

Thus, numeric OR is now +|. Ugly, yes, but how often do you use these? Besides, it may be ugly, but it's very clear to anyone who reads the code what $foo +| $bar will do.

By the time you reach this paragraph, you either want to know what tilde has to do with all of this, or you have already thought of it.

Tilde is for strings what plus is for numbers. That is: infix it will add (concatenate), prefix it will force string context (to get rid of "$foo" and $foo . "". And in bit operators it will give you string bit operations.

Some examples:

"foo" ~ "bar" # >>> "foobar" 5 ~ 09 # >>> "59" ~1e3 # >>> "1000" "ja" ~| " ph" # >>> "japh"

Some want the dot to stay for string concatenation. That would be possible, but the dot as a prefix operator would be troublesome with numbers:

"foo" . "bar" # >>> "foobar" 5 . 09 # >>> "59" .1e3 # >>> "1000" "ja" .| " ph" # >>> "japh"

You do want .1 to be 0.1 rather than "1", I think. And, you may have been used to writing spaces around the dot before, but many people were surprised that while $foo.$bar could result in "53", 5.3 never did.

The dot isn't lost, though. Perl 6 uses it for object orientation, instead of the arrow that Perl 5 used. It can even be used prefix, because method names cannot begin with a digit. Of course, when used without a left side, it defaults to $_. In my code, at least, method calls are used much more often than string concatenation. I like the change because of that, and because of the nice consistency with plus, that tilde can give us.

Sep 15, 2005 at 14:58 UTC, tye: Moved from Tutorials to Meditations (edit:14, keep:1)

Replies are listed 'Best First'.
Re: (P6) Why the tilde makes sense
by itub (Priest) on Sep 09, 2005 at 15:10 UTC
    5 ~ 09          # >>> "59"

    I'm curious, is perl6 also dropping octal numbers? Because the equivalent example in perl5 would complain "Illegal octal digit '9'".

      Since octals are less important than they used to be in PDP-11 days, Perl 6 is changing octals to be consistent with hex notation: you have to say 0o777 instead of 0777. (Or use the general radix form, 8:777.)
        The thing about it is that the rest of computerdom interprets 0777 as an octal number. I'm not saying that what's happening here is bad, but I don't understand the drive break convention.

        thor

        Feel the white light, the light within
        Be your own disciple, fan the sparks of will
        For all of us waiting, your kingdom will come

      Quoting from S02:

      Initial 0 no longer indicates octal numbers. You must use an explicit radix marker for that. Pre-defined radix prefixes include:
          0b          base 2, digits 0..1
          0o          base 8, digits 0..7
          0d          base 10, digits 0..9
          0x          base 16, digits 0..9,a..f (case insensitive)
      

Re: (P6) Why the tilde makes sense
by castaway (Parson) on Sep 10, 2005 at 16:24 UTC
    I'm not quite sure why you explained where the plus went, since the original choice for string addition was the underscore .. So why did that particular one get disgarded?

    C.

      Several reasons. You can cuddle a ~ up to identifiers, and it doesn't disappear visually when you do so. Unary ~ came available when we unified all the XORly operators under ^ (including old unary ~, that is, 1's-complement, which implicitly XORs all the bits with 1). Plus underscore basically comes across as a very short identifier, and we wanted to reserve it for possible use as some kind of "don't care" token in logic programming, as it's used in Prolog. Or maybe we'll think of some other use for it. (I can guarantee you that it won't be the current stat buffer as it is in Perl 5, though, since you can put a stat result into any variable now.)

      But, of course, the main reason is that ~ looks like a piece of string. :-)

      I'm not quite sure why you explained where the plus went, since the original choice for string addition was the underscore .. So why did that particular one get disgarded?

      I haven't tried explaining why the tilde glyph is used, but instead why having a universal glyph for string operations is useful. Sorry if the title mislead you.

      I do say something about why that glyph isn't dot, but even for that I'm not mentioning all there is: the small size of the dot makes it too easy to overlook or to be a semi-meta-operator and OO takes precedence over concatenation.

      Underscore has many of the problems dot has, and added to that the many problems that a \w character brings along.

      I explain plus first, because tilde is the string version of that operator, and I think the consistency in that is very important, and much easier to explain by waiting with the promised subject. Otherwise, the explanation of plus would have come after, and not be an essential part of the article anymore, but instead an appendix that gets less attention.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }