On 3/27/06, Jim <[EMAIL PROTECTED]> wrote: > > > > > > I am trying to understand how vec() is works in the code below > > > > Have you seen the documentation in the perlfunc manpage? > > > > perldoc -f vec > > > > If you still have questions, after seeing the docs, please ask again. > > Hope this helps! > > > > Yes, I have read it already. Thanks >
Yeah, the vec() manpage is one of the few pieces of true cruft in the perldocs for builtins. I guess it's because it's one of those functions that you really only use if you already know how to use it. I know what it does and the manpage still doesn't make sense to me.... For now, just read the first sentence, and then stop: Treats the string in EXPR as a bit vector made up of elements of width BITS, and returns the value of the element specified by OFFSET as an unsigned integer. You might also want to keep in mind that the BITS must be a power of 1 from one to 32, and that vec() is lvaluable, but you don't need to know that to look at the code here. vec( $address, 7, 1 ) sayas "break up the string $address into chunks of one bit, and return the seventh chunk". Then it looks at the 6th chunk and the 5th chunk. The sneaky part was stringing all those tertiary "hook" operators together; that's probably the confusing part. John, or whoever wrote the code, knows that those particular bits will be empty for networks of various classes, and the test falls through until it gets a hit or defaults to 'D'. For clarity, you could rewrite this as if ( ! vec($address, 7,1) ) { $class = A; } elsif ( ! vec($address,6,1) ) { $class = B; } elsif ( ! vec($address,5,1) ) { $class = C; } else { $class = D; } It really executes, though, more like: if ( vec($address,7,1 ) { if ( vec($address,6,1) { if ( vec($address,5,1) { $class = 'C'; } else { $class = 'D'; } } else { $class = 'B'; } else { $class = 'A'; Why this works has to do with the way different classes of networks are assigned, and the way struct in_addr is defined in socket.h. For more information on the various standard ways hostnames and inet address are assigned and described, pick up a book on tcp/ip. HTH, -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!