Mailing List Archive

Re: '&' operator on strings
In article <ukwx9c5x0n.fsf@linda.teleport.com> you write:

>Unfortunately, there are two ways to think of bit operators on strings:
>
>(1) interpret the strings as numbers, convert the numbers to integers,
>perform the operation, and then re-float that back to a number, or
>(2) interpret the strings as *bit patterns*, and perform the bit operation
>directly on strings.
>
>For many moons (perl 2 to perl 4), Perl used interpretation #1
>*unless* there was a vec() present in the program, and then it used
>interpretation #2. In an effort to eliminate "unexpected action at a
>distance" from perl 5, Larry made the bit-ops *always* use
>interpretation #2. If you want to bit-and the numeric value of the strings,
>just make them a number yourself:
>
> $answer = (0+"124.3") & (0+"99");
>
>Sure, it's a little more hassle, but well worth the elimination of
>"action at a distance".
>
>Yes, this means that:
>
> print "01" & "1";
>
>will print 1 in perl4 (in absence of a vec), and 0 in perl5. Oh well.

This is an excellent explanation: I humbly suggest that it's included in
the Fine Manual. Currently neither perlop nor perltrap appear to mention
these subtleties. It's definitely a trap, since a legal perl 4 program
can produce non-obviously wrong results, without warning.

Perl4->5 traps will be needed for at least as long as perl 4-based books
are in print: i.e. a while yet.

Ian