writch has asked for the wisdom of the Perl Monks concerning the following question:
The results were NOT what I expected. I'd have been happier if they returned '42' instead of '96', but '24' is what is supposed to come out. Here's me running it in the debugger:#!/usr/bin/perl use strict; use warnings; # a simple script to produce the 'or' number required for the debug_le +vel setting # in nagios.cfg my $or; for my $x (@ARGV){ $or |= $x ; } printf("%d\n", $or); exit;
It's mystified the other perl programmer at my office as well. What's going on here?perl -d or 16 8 Loading DB routines from perl5db.pl version 1.37 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(or:9): my $or; DB<53> n main::(or:10): for my $x (@ARGV){ DB<53> n main::(or:11): $or |= $x ; DB<53> x $or, $x 0 undef 1 16 DB<54> x $or|$x 0 16 DB<55> n main::(or:11): $or |= $x ; DB<55> x $or, $x 0 16 1 8 DB<56> x $or|$x 0 96 DB<57> x 16|8 0 24 DB<58> n main::(or:14): printf("%d\n", $or); DB<58> n 96 main::(or:16): exit;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Counter Intuitive Code
by Corion (Patriarch) on Mar 27, 2019 at 14:41 UTC | |
Re: Counter Intuitive Code
by roboticus (Chancellor) on Mar 27, 2019 at 17:41 UTC | |
Re: Counter Intuitive Code (use feature 'bitwise')
by LanX (Saint) on Mar 27, 2019 at 23:23 UTC | |
Re: Counter Intuitive Code
by VinsWorldcom (Prior) on Mar 27, 2019 at 15:02 UTC | |
Re: Counter Intuitive Code
by LanX (Saint) on Mar 27, 2019 at 14:43 UTC | |
Re: Counter Intuitive Code
by vr (Curate) on Mar 27, 2019 at 22:19 UTC | |
by RonW (Parson) on Mar 31, 2019 at 17:31 UTC |