[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Bit-wise logical operators in MacPerl



I have found something which strikes me as strange and wonder if anyone can
shed light on it?

The statements below will return 1 for true as follows:

1)  (/foo/ | /bar/)     for a string with "foo" or "bar" or both
2)  (/foo/ ^ /bar/)     for a string with "foo" or "bar" but not both
3)  (/foo/ & /bar/)     only for a string with both "foo" and "bar"

This is exactly what one would expect. For instance in 3) a match for "foo"
(returning 1) and a match for "bar" (returning 1) would be necessary for
the whole condition to be met since 1 & 0 == 0.

Equally one can put the 'short-cut' operators || and && in 1) and 3) for
similar reasons. So far so good.

But the condition <if(/foo | bar/)> is also met for a line containing
either "foo" or "bar" or both. (Try it and see.) Why?

Suppose "foo" were "A" and "bar" were "B". The bit-wise operator | would
(presumably) take that to mean 65 | 66 would it not? But 65 | 66 == 67,
which is certainly not "true".

Why does (/foo | bar/) work in this way? Neither (/foo & bar/) nor (/foo ^
bar/) nor (/foo && bar/) nor (/foo || bar/) behave other than as expected.
Is this:

        a)      a happy quirk of Unix Perl?
        b)      a built-in undocumented property of Unix Perl?
        c)      something unique to MacPerl?
        d)      just something I have missed in the documentation?

Certainly it is extremely useful -- but is it safe?

Alan