For the same reason Perl 5 has '+'and '.': because of the extremely convenient auto-conversion of strings to numbers:
# in Perl 5
$i=10; # could be $i="10";
$j=1; # could be $j="1";
print $i + $j; # 11
print $i . $j; # 101
| [reply] [d/l] |
most languages (that I know) use the + for both
Yes, but other languages don't have Perl's notion of context. In Perl, the + operator supplies numeric context, but the concatenation operator supplies string context. The difference can be important.
my $barcode_prefix = "25728";
for my $b (@series) {
my $chkd = checkdigit($barcode_prefix . $b);
}
If you change the concatenation operator there to +,
a completely different thing happens and the check
digit comes out wrong. (And yes, I have written
code that calculates barcode check digits in Perl5.)
| [reply] [d/l] |
In those languages you must do somthebg like int($a) + int($b) if you have read the numbers from a file, in Perl you can add them whitout telling them to be numbers first. It also means there must be a diffrent concat operator. | [reply] [d/l] |
I'm not sure how it'd work what with Perl constantly auto-stringifying and auto-numberfying things. I'd think we need separate numeric-add and string-concatenate operators for the same reason we need separate == and eq. | [reply] [d/l] [select] |
That's an easy one...
- '+' should be associative (like in mathematics).
- Concatenation isn't associative.
- Therefore, concatenations shouldn't be denoted by '+'.
| [reply] |
| [reply] |
For those of us mere mortals.
- Given a set A with members a, b, c, and an operator *, * is said to be an associative operation iff (a * b) * c = a * (b * c) for arbitrary a, b, c within A. Even plainer: the order in which you take operations doesn't matter.
- Given a set A with members, a, b, and an operator *, * is said to be a commutative operation iff a * b = b * a for arbitrary a, b within A. Even plainer: operand order doesn't matter.
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
<
| [reply] |