Looking for explanation

C

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--
 
G

Gunnar Hjalmarsson

ccannick said:
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.
 
B

Ben Morrow

Quoth ccannick said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top