Hello all, I am fairly new to perl, having learned it 10 years ago by self training, but then didn't use it until recently. I have some to ping an IP address using a subroutine that works with an array, or with an IP from STDIN, but not from a straight variable. I'm confused! Any help would be appreciated! Thanks! Here are 3 examples of the code, 2 that work, and the one I really need to work that doesn't:

# this does not work use strict; use Net::Ping; use warnings; my $ip = qw(172.16.1.2); # my $works = &pingIp($ip); # if ( $works == 1 ) { print "does not ping\n"; } else { print "pings\n"; } sub pingIp { my $ip1 = shift; my $p = Net::Ping->new(); if ($p->ping($ip1)) { print "return is 0\n"; return 0; } else { print "return is 1\n"; return 1; } $p->close(); }

If I prompt for an IP rather than set the IP outright, the above works

print "Enter an IP: "; chomp (my $ip=<STDIN>);

Or, if I create an array of IPs and ping each one using the pingIp subroutine, it works

my @IParray = qw(172.16.1.2 172.16.1.3); foreach my $ip (@IParray) { my $works = &pingIp($ip); if ( $works == 1 ) { print "$ip does not ping\n"; } else { print "$ip pings\n"; } }

Thanks for any help!


In reply to Ping not working by ja6025

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.