[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

[ID 20000125.001] Problem with Fcntl constants



I noted the following problem and posted a query in the perl.misc newsgroup.
This code:

   my $sum = LOCK_EX + LOCK_NB;
   my $or = LOCK_EX | LOCK_NB;
   print "$exclusive + $non_block = $sum\n";
   print "$exclusive | $non_block = $or\n";

prints the following results:

   2 + 4 = 2
   2 | 4 = 6

Both Randal Schwartz and Larry Rosler correctly diagnosed the problem.
Here's Randal's (edited) description:

>Watch:
>
>    use Fcntl qw(LOCK_EX LOCK_NB);
>
>    my $ex = LOCK_EX;
>    my $nb = LOCK_NB;
>
>    for (split /\n/, <<'END') {
>    LOCK_EX
>    LOCK_NB
>    $ex
>    $nb
>    LOCK_EX + LOCK_NB
>    $ex + $nb
>    LOCK_NB + LOCK_EX
>    $nb + $ex
>    LOCK_EX() + LOCK_NB()
>    LOCK_EX(+ LOCK_NB)
>    END
>      print "$_ is ", eval $_, "\n";
>    }
>
>Which prints:
>
>    LOCK_EX is 2
>    LOCK_NB is 4
>    $ex is 2
>    $nb is 4
>    LOCK_EX + LOCK_NB is 2
>    $ex + $nb is 6
>    LOCK_NB + LOCK_EX is 4
>    $nb + $ex is 6
>    LOCK_EX() + LOCK_NB() is 6
>    LOCK_EX(+ LOCK_NB) is 2
>
>Ahh... look at the last two.  They give away the secret.  LOCK_EX is
>defined as "sub LOCK_EX { 2 }".  So "LOCK_EX + LOCK_NB" is being
>parsed as "LOCK_EX(+ LOCK_NB)" - a unary plus operator in front of the
>ignored argument!
>
>These subs *should* have been prototyped as "sub LOCK_EX () {2}".  But
>no.

When I Emailed Randal to ask whether this has been reported, I didn't
understand his reply (he referred to a followup message that as far as I can
see doesn't seem to exist, or at least my news server isn't showing it).  So
rather than take up any more of his time, I'm just going ahead and reporting
it as a Perl bug to you.

            -- Warren


Follow-Ups from:
merlyn@stonehenge.com (Randal L. Schwartz)

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]