Looking for explanation
am 22.01.2008 00:18:54 von ccannick
Don't do much perl programming. Can someone answer a few questions
for me?
Here's a subroutine I'm looking at:
sub check_cs_services
{
foreach $service (sort keys %service_def) {
my $sock = new IO::Socket::INET (
PeerAddr => $cs_primary_ip,
PeerPort => $service_def{$service},
Proto => 'tcp'
);
if($sock) {
$cs_service{$service} = 1;
close($sock);
}
}
}
The if block at the end is unclear to me. What is if evaluating as
true? Just the existence of $sock or is it something I don't
understand?
Thanks for your help.
--C--
Re: Looking for explanation
am 22.01.2008 00:56:20 von Gunnar Hjalmarsson
ccannick wrote:
> Can someone answer a few questions for me?
I only noticed one question.
> if($sock) {
> $cs_service{$service} = 1;
> close($sock);
> }
> The if block at the end is unclear to me. What is if evaluating as
> true? Just the existence of $sock or is it something I don't
> understand?
It tests whether $sock contains a true value.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: Looking for explanation
am 22.01.2008 01:10:37 von Ben Morrow
Quoth ccannick :
>
> sub check_cs_services
> {
> foreach $service (sort keys %service_def) {
> my $sock = new IO::Socket::INET (
> PeerAddr => $cs_primary_ip,
> PeerPort => $service_def{$service},
> Proto => 'tcp'
> );
>
> if($sock) {
> $cs_service{$service} = 1;
> close($sock);
> }
> }
> }
>
> The if block at the end is unclear to me. What is if evaluating as
> true? Just the existence of $sock or is it something I don't
> understand?
$sock will be true if and only if the socket could be successfully
created. If the connection failed for any reason, $sock will be undef.
It looks like the program is trying to detect which ports are open on
$cs_primary_ip?
Ben