Kaz Kylheku <
221-50...@kylheku.com> writes:
> On 2016-09-19, Ben Bacarisse <
ben.u...@bsb.me.uk> wrote:
>>
gaz...@shell.xmission.com (Kenny McCormack) writes:
>><snip>
>>> Actually, I'm pretty surprised that no one has posted a Perl solution
>>> yet.
>>
>> Perl has an operator that increments a string: Y -> Z -> AA -> AB so it
>> may be a natural fit for the OP depending on the actual task at hand:
>>
>> perl -e '$l="Q";print++$l'
>
> I don't use perl, but I "manually fuzzed" the above and found
> two language design cluster-fucks in about 10 times that many
> seconds:
>
> $ perl -e '$l="Q";print ++$l, "\n"'
> R
>
> $ perl -e '$l="Q";print $l+1, "\n"'
> 1
Read the Perlop man page.
No where does it say that "++" is another way of adding.
What it does say is:
The auto-increment operator has a little extra builtin magic to it.
If you increment a variable that is numeric, or that has ever been
used in a numeric context, you get a normal increment. If, however,
the variable has been used in only string contexts since it was set,
and has a value that is not the empty string and matches the pattern
"/^[a-zA-Z]*[0-9]*\z/", the increment is done as a string, preserving
each character within its range, with carry...
and then:
The auto-decrement operator is not magical.
As long as the behavior is documented, I'm fine with it.
> Inconsistency: the result of ++A is not A + 1.
>
> $ perl -e '$l="Q";print $l+1, " ", ++$l,
> "\n"'
> 1 1
>
> Unsafe behavior: arithmetic operator + has apparently performed a side
> effect on $l, changing the outcome of ++$l.
Man page says:
Note that just as in C, Perl doesn't define when the variable is
incremented or decremented. You just know it will be done sometime
before or after the value is returned. This also means that modifying
a variable twice in the same statement will lead to undefined
behavior. Avoid statements like:
$i = $i ++;
print ++ $i + $i ++;
Perl will not guarantee what the result of the above statements is.
As long as the behavior is documented, I'm fine with it.
--
Dan Espen