Path: utzoo!utgpu!watserv1!watmath!att!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!jarthur!uunet!convex!convex.com!tchrist From: tchrist@convex.com (Tom Christiansen) Newsgroups: comp.lang.perl Subject: bit vectors, lvalues, and magical relational operators Message-ID: <108948@convex.convex.com> Date: 19 Nov 90 16:47:05 GMT Sender: news@convex.com Reply-To: tchrist@convex.com (Tom Christiansen) Organization: Convex Computer Corp, Richardson, TX Lines: 90 Here's three postings in one: 1. BIT VECTORS I've been playing with bit vectors (which are really pretty neat as long as you're careful not to == them), and I'm not entirely clear on what the third value is the in the vec() call. I thought it was a length analogous to substr, but empirical evidence suggests otherwise. I looked briefly at do_vec(), and it looks like you're using it as some sort of multiplier. Could you shed any light here? The only one of these that gives results I understand is the first one: vec($a, 40, 1) = 1; &pvec($a); vec($b, 40, 4) = 1; &pvec($b); vec($c, 40, 1) = 4; &pvec($c); vec($d, 40, 4) = 4; &pvec($d); sub pvec { local($v) = @_; local($i); local($len) = length($v); printf "%5d: ", $len; print "\n"; return; for ($i = 0; $i < ($len*8); $i++) { print vec($v,$i,1); print ' ' if ($i + 1) % 8 == 0; } print "\n"; } And how come I can do |, &, ^, and ~ on bits, but not << or >>? 2. LVALUEs On lvalues, I thought assign returned an lvalue, but that's not quite so in all contexts, is it? None of these work: 1. ($a = $b) = $c; 2. chop(@list = <>); 3. ($x ? $a : $b) = $c; Even though these are just fine: 4. ($a = $b) =~ tr/a-z/A-Z/; 5. chop($a = $b); 6. print ++($a = 'red'); # "ree" I don't see why 5 should work and 2 not, nor why 6 works but 1 and 3 do not. I'm sure the answer is buried deep inside perl.y somewhere. 3. MAGICAL RELATIONAL OPERATORS Here's another idea: the in the spirit of || and && being a bit magical by allowing you do say: $a = $b || $c; instead of ($a = $b) || ($a = $c); how about having relational operators like < and > being defined to return one of their operands in such a way as to make this possible: if ($a < $b < $c) if ($a > $b > $c) They're already leftly-associative, so that's ok. All they'd need to do is return return their left-hand operand if true instead of 1. How many scripts would this break? I think people usually do if ($a < $b) which would still work fine, instead of : $a = 1 + ($b > $c); which would probably not. Of course, once you've done this, you'd want the same treatment on "<=", ">=", "lt", "le", "gt", and "ge" as well. How does that sound? --tom