Looking for explanation

Discussion in 'Perl Misc' started by ccannick, Jan 21, 2008.

  1. ccannick

    ccannick Guest

    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, Jan 21, 2008
    #1
    1. Advertising

  2. 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, Jan 21, 2008
    #2
    1. Advertising

  3. ccannick

    Ben Morrow Guest

    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
    Ben Morrow, Jan 22, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mariusz

    explanation

    Mariusz, Jan 12, 2004, in forum: VHDL
    Replies:
    1
    Views:
    574
    tbx135
    Jan 13, 2004
  2. responsible
    Replies:
    4
    Views:
    322
    red floyd
    Aug 5, 2008
  3. Dark Ambient
    Replies:
    2
    Views:
    85
    Jim Cochrane
    Jul 22, 2006
  4. Replies:
    9
    Views:
    205
    Keith Thompson
    Mar 10, 2013
  5. Haircuts Are Important
    Replies:
    10
    Views:
    291
    Ike Naar
    May 17, 2013
Loading...

Share This Page