I'm trying to work through a small Perl learning project that requires reading 4 unsigned integers from a socket. I couldn't get more than 1 integer read, and after digging around I found a solution. But I NEED to understand what I didn't do right (and have gone through a couple of Perl books, perldocs, etc to no avail.)
Example 1: Here's the successful solution code (original), assume the socket connect is successful for both below:
{
local $/ = \16; # make <> read in 16 bytes with one swoop.
my @integers = unpack "IIII", <$sock>;
print "numbers: @val\n";
}
Example 2: I tried this below. If I print the input prior to unpacking, I only get one Integer:
my $input;
$sock->recv($input,16,0);
my @integers = unpack("IIII", $input);
Specific questions:
- In example 1, what the heck is "$/"? And how does it "change" <>, which I thought read STDIN?
- In example 2, is there some reason why my recv() doesn't take more than one integer off the socket? My understanding (per perldoc) is that the "SIZE" parameter defaults to "bytes", and integers are 4 bytes?
Any help, pointers, etc. is appreciated. Btw, the "learning project" is overthewire.org - pretty cool stuff.