Hey Guys
I'm trying to read an answer from an SMTP server via socket. I'm writing a function wich returns everything till it finds a line which doesn't begin with 3 digits and a - (/^[\d]{3}-/), but returns this line,too. Best I could get till now, is the function below. My question now is, if that one is good or anybody knows a better solution.(but no Net::SMTP package or so pls) I read somewhere that if I use select, i shouldn't use the "readline" with <FH>, but with sysread() it gets much more complicated I guess...
Thanks R.B.
Here's the code: sub getSMTPResponse { my ($sock) = @_; my ($rin, $response, $i); my ($timeout) = 5;
vec($rin="", fileno($sock), 1) = 1 ; # create bitmask with the socket
if (select($rin, undef, undef, $timeout)) { # check if socket is ready do { $i = <SOCK>; # read in a line $response .= $i; # append to result } while($i =~ /^[\d]{3}-/); return($response); # return result
Seems like the 'do...while' is causing the issue here because it executes first then does the test. Seems like it would be sufficient and maybe more clear to use a straight while and postponed concatenation. Also seems that you should be reading from $sock instead of SOCK, but I am not clear on that...
while (my $line = <$sock>) { last if ($line =~ /^\d{3}-/); $response .= $line; } return $response;
} else { return(undef); # timeout on select occurred } }
Helps?
http://danconia.org
I assume you have read the warning about using select and buffered I/O in the perldoc -f select page.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>