Looking for explanation

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--
ccannick [ Di, 22 Januar 2008 00:18 ] [ ID #1913478 ]

Re: Looking for explanation

ccannick wrote:
> Can someone answer a few questions for me?

I only noticed one question.

<snip>

> 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
Gunnar Hjalmarsson [ Di, 22 Januar 2008 00:56 ] [ ID #1914052 ]

Re: Looking for explanation

Quoth ccannick <ccannick [at] gmail.com>:
>
> 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
Ben Morrow [ Di, 22 Januar 2008 01:10 ] [ ID #1914054 ]
Perl » comp.lang.perl.misc » Looking for explanation

Vorheriges Thema: FAQ 9.24 How do I fetch a news article or the active newsgroups?
Nächstes Thema: What's the best way to test existence of arguments