There's probably more than one way to accomplish the goal in the title -- and I'd love to hear stories about it, if only because it'd make me feel better.
I had the following, very simple line of code:
$user = ~ s/^\s+|\s+$//gs;
The intention, of course, was to trim white space from the beginning and end of a user name previously parsed from a record. Instead, though, $user ended up containing a large and random-seeming number.
Spot the bug?
Unary "~" performs bitwise negation, i.e., 1's complement. For example, 0666 & ~027 is 0640. (See also "Integer Arithmetic" and "Bitwise String Operators".) Note that the width of the result is platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64 bits wide on a 64-bit platform, so if you are expecting a certain bit width, remember to use the & operator to mask off the excess bits. -- Symbolic Unary Operators
Yes, a single space between my = and my ~ resulted in my intended "match" operation instead assigning a numeric value to $user. $head .= $desk
Stepping through the code with the debugger found the line where the oddness was occurring, but it seemed like inexplicable madness (for something like 10 minutes) until I noticed that one extra space. Man, do I feel stupid!
The lesson? I probably should have used Text::Trim. :)
|
|---|