I have a strong suspicion that that isn’t going to do what I think it is wanting to do. You’re asking for four things, but $digits will contain just one. You would split on a literal \. etc, but the Socket::inet_aton solution provided below is better.
–
tchristJun 12 '11 at 13:18

1

Check the code again. What you have won't compile. Even after you fix that issue, I am sure it doesn't match the book. Based on the many positive reviews that book got, I'd be very surprised if Meltzer and Michalski would advocate changing the value of $ENV{REMOTE_ADDRESS}.
–
daotoadJun 12 '11 at 15:29

He's not changing the environment variable. The idea is to copy the value into $digits and modify the copy. Which doesn't mean that the whole code snippet is not nonsense, because I think it is.
–
bartJun 12 '11 at 21:39

To the OP: can you point out where in the book you found that code? Because I have a strong feeling you messed something up.
–
bartJun 12 '11 at 21:45

In page 61. Even correct #ENV to $ENV, this code does not create record in database.
–
WeiyanJun 13 '11 at 13:55

Your code looks like a mangled extract from code to convert IP addresses in dotted-quad format to a string of four bytes that can be passed to a socket call, for instance.

Assuming that #ENV is a mistake for $ENV, your code strips out the dots from the dotted quad, giving you a string of 4 to 12 digits. It then (implicitly) interprets those digits as a single integer, putting its least significant 8 bits in the first byte of the result and filling the rest with zeroes (because pack supplies empty values for the three missing list elements, which are then interpreted as zeroes).

Why you would want to print a binary IP address to a log file, I won't attempt to guess. But the code as given is close to nonsense. If you do need a binary IP address for something, I recommend