Hi perl monks,
I am a newbie in the forum. I was following the forum and today finally I became member of this fantastic forum. I was trying out the module "IO::Socket::PortState qw(check_ports);" It is perfectly working without reading a file:
#!/usr/bin/perl -w use strict; use IO::Socket::PortState qw(check_ports); my $proto = 'tcp'; my $port = '8307'; my $address = '127.0.0.1'; my %hash = (); #########This works ######################################## my($section, $ping_timeout, %porthash); $porthash{$proto}{$port}{'name'} = $section; print "here is the $port"; check_ports($address, $ping_timeout, \%porthash); my $open = $porthash{$proto}{$port}{'open'}; if ($open) { print "alive\n"; } else { print "dead\n"; } #################################################################
output is :
"alive"
and the text file contains:
port1 = 8307
port2 = 15677

But when I want to read my ports from a file, results are for each port "dead", Here is the code:
#!/usr/bin/perl -w use strict; use IO::Socket::PortState qw(check_ports); my $proto = 'tcp'; #my $port = '8307'; my $address = '127.0.0.1'; my %hash = (); open( OUT, "<location of the file>" ) || die "Problems during the writing $!"; while (<OUT>) { %hash = (); my ( $key, $port ) = split( '=', $_ ); $hash{$key} = $port; my ( $section, $ping_timeout, %porthash ); $porthash{$proto}{$port}{'name'} = $section; check_ports( $address, $ping_timeout, \%porthash ); my $open = $porthash{$proto}{$port}{'open'}; if ($open) { print "port : $port alive\n"; } else { print "port : $port dead\n"; } }
output is :
port : 8307
dead
I really don't get it. why the port 8307 (listening) is working with the previous script and not when I read all those ports from a file?
Could you help me out from this issue?
Thanks in advance

In reply to Re^2: Usage of IO::Socket::PortState by perl_dedicted

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.