1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl -w # This scrit use the Net::Ping Module to ping an # host (value of $ARGV[0]). The protocol is icmp, # timeout is 5 seconds, and bytes sent are 256. # # Example: # # [root@localhost scripts]# perl ping.pl google.com # google.com up use Net::Ping; $p = Net::Ping->new("icmp", 5, 256); if($p->ping($ARGV[0])) { print "$ARGV[0] up\n"; } else { print "$ARGV[0] down\n"; }
Refactorings
No refactoring yet !
Marco Valtas
March 18, 2008, March 18, 2008 04:01, permalink
I think there's not much to say about this code, I've just changed the (") to (') in the ICMP because you don't need to interpolate a variable there, and made the call directly using a chain call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/perl -w # This scrit use the Net::Ping Module to ping an # host (value of $ARGV[0]). The protocol is icmp, # timeout is 5 seconds, and bytes sent are 256. # # Example: # # [root@localhost scripts]# perl ping.pl google.com # google.com up use Net::Ping; if(Net::Ping->new('icmp', 5, 256)->ping($ARGV[0])) { print "$ARGV[0] up\n"; } else { print "$ARGV[0] down\n"; }
Daniel Fisher
June 30, 2008, June 30, 2008 13:13, permalink
You can set a '$default_host_name' and this uses the ternary '[if]?[then]:[else]' operator to be more concise without (hopefully) being to obscure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/perl -w # This script use the Net::Ping Module to ping an # host (value of $ARGV[0]). The protocol is icmp, # timeout is 5 seconds, and bytes sent are 256. # # Example: # # [root@localhost scripts]# perl ping.pl google.com # google.com up use Net::Ping; my $default_host_name = 'google.com'; my $host_name = $ARGV[0] || $default_host_name; my $response = Net::Ping->new('icmp', 5, 256)->ping($host_name) ? 'up' : 'down'; print "$host_name $response"."\n";
Simple example