afongen
Sam Buchanan's weblog.

chomp

Gah. I just spent more time than I'd care to admit working on a stupid little bug. I'm writing a module that opens up a socket, sends a request (remote procedure call, really) as XML, gets an XML response, and closes the socket. Simple enough, except that the XML I was getting back didn't quite seem right. It looked fine if I picked it off the stream byte by byte, but if I just slurped the whole thing up ($str = <$socket> — it's just a couple hundred bytes, after all) it was all messed up. At first I figured maybe it was a buffering problem, even though that didn't quite make sense, but no. I tried to grab the response line by line:

$/ = CR;
while (my $buf = <$socket>( {
  $response .= $buf;
}

But no. When I tried to print "$response\n", it was a mangled mess. My tests were fine, but it didn't look right.

You're probably seeing the problem already: I wasn't stripping the carriage return, so it didn't print properly on Windows or Unix, which use CRLF and LF, respectively. Duh.

$/ = CR;
while (my $buf = <$socket>( {
 chomp $buf;
 $response .= $buf;
}

Dumb, dumb, dumb. This is the sort of thing that I would have picked up on right away, were I not scattered across too many projects for me to focus properly. I should have trusted my tests and just moved on. I'm not sure why I felt compelled to print the response to the terminal, in the first place. This'll teach me.