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 | |
by TimToady (Parson) on Sep 09, 2005 at 16:50 UTC | |
by thor (Priest) on Sep 09, 2005 at 19:01 UTC | |
by TimToady (Parson) on Sep 09, 2005 at 20:39 UTC | |
by chester (Hermit) on Sep 09, 2005 at 19:51 UTC | |
by kelan (Deacon) on Sep 09, 2005 at 18:09 UTC | |
|
Re: (P6) Why the tilde makes sense
by castaway (Parson) on Sep 10, 2005 at 16:24 UTC | |
by TimToady (Parson) on Sep 10, 2005 at 17:37 UTC | |
by Juerd (Abbot) on Sep 12, 2005 at 08:51 UTC |