• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Newbie-- HELP! : (F) Perl was trying to evaluate a reference to a code value (that is, a subroutine), but found a reference to something else instead.

Can someone PLEASE tell me what is wrong with this code.  I've been trying to figure it out for 2 hours.
i just started learning perl.  I've never seen any error like this. Not even in C.

#!/usr/bin/perl -w

###########################
###### Description     ############
### A basic site pinger ############
### ########################
###########################

use strict;
use warnings;
use diagnostics;

use Net::Ping;

if (@ARGV)
   {
      my $host = shift;
      chomp($host);
      my $err = pingIt($host);
   }
else
   {
    print "usage: pingtest <hostname>";
   }


 sub pingIt
 {
     my $p = Net::Ping->new("icmp");
     my $retStat = $p->($_[0], 4);
     $p->close();
     return($retStat);
 }
0
TristinColby
Asked:
TristinColby
  • 2
1 Solution
 
tone28Commented:
#!/usr/bin/perl -w

###########################
###### Description     ############
### A basic site pinger ############
### ########################
###########################

use strict;
use warnings;
use diagnostics;

use Net::Ping;

if (@ARGV)
   {
      my $host = shift;
      chomp($host);
      my $err = pingIt($host);
        if($err) {
            print $err;
          }
        else {
            print "Not init";
        }
   }
else
   {
    print "usage: pingtest <hostname>";
   }


 sub pingIt
 {
     my $p = Net::Ping->new("icmp");
     my $retStat = $p->ping($_[0], 4);
     $p->close();
     return($retStat);
 }



in the pinigIt sub you forgot to add ping to the p->($_[0],4)  --> p->ping($_[0],4)


0
 
mjcoyneCommented:
sub pingIt
 {
     my $p = Net::Ping->new("icmp");
     my $retStat = $p->ping($_[0], 4);
     $p->close();
     return($retStat);
 }
0
 
mjcoyneCommented:
Oops -- tone28 beat me to it.  Note that you might also want to scope $host and $err outside of the "if" loop if you'll want to use them elsewhere:

#!/usr/bin/perl -w

###########################
###### Description     ############
### A basic site pinger ############
### ########################
###########################

use strict;
use warnings;
use diagnostics;
use Net::Ping;

my ($host, $err);

if (@ARGV)
   {
      $host = shift;
      chomp($host);
      $err = pingIt($host);
   }
else
   {
    print "usage: pingtest <hostname>";
   }

 sub pingIt
 {
     my $p = Net::Ping->new("icmp");
     my $retStat = $p->ping($_[0], 4);
     $p->close();
     return($retStat);
 }
0
 
TintinCommented:
I'd write it as

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Net::Ping;

die "USAGE: $0 <hostname>\n" unless @ARGV;
my $err = pingIt(@ARGV);

sub pingIt {
     my $host = shift;

     my $p = Net::Ping->new("icmp");
     my $retStat = $p->ping($host, 4);
     $p->close();
     return $retStat ;
}
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now