in reply to Re^2: String eval is cool
in thread String eval is cool

IMO the eval version is much clearer than anything involving pack/unpack could be.

True, but that's why it was isolated into a small function with a clear purpose. Modularisation introduces ease of testing, which allows for hiding of complicated code. Have you looked at the guts of the modules you use? You'll find similar complexity.

Speaking of, this should be done with a module such as NetAddr::IP.

On a 32 bit machine the results are undefined.

Oops. I'm used to doing these operations in assembler where portability is a non-issue, so I changed my solution while posting. My original solution (follows) doesn't suffer from that problem, and also works on 64-bit perls unmodified:

$mask = 0xFFFFFFFF >> (32-$bits) << (32-$bits);