From dffd9eef2d78b300c3bddb8b4282e03579e94948 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sat, 23 Feb 2013 21:33:20 -0500 Subject: [PATCH] Use Getopts::Std For Command Option Parsing --- sbin/pinger | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/sbin/pinger b/sbin/pinger index 384675c..e77bc8d 100755 --- a/sbin/pinger +++ b/sbin/pinger @@ -1,39 +1,31 @@ #!/usr/bin/env perl use strict; +use Getopt::Std; use Net::Ping; use POSIX; use Socket; +use Switch; use Time::HiRes qw/time/; if($> != 0) { die("pinger must be run as root\n"); } +my %switches; my $alive = 1; my $time = 0; my $name = 0; my($start,$finish); -while($ARGV[0] =~ /\-/) { - my $arg = shift(@ARGV); - $arg =~ s/-//; - my @switches = split('', $arg); - - foreach my $switch (@switches) { - if($switch eq "d") { - $alive = 0; - } elsif($switch eq "r") { - $alive = 1; - } elsif($switch eq "n") { - $name = 1; - } elsif($switch eq "t") { - $time = 1; - } elsif($switch eq "h") { - help(); - } else { - print "pinger: invalid option -- '$switch'\n"; - help(); - } +my $status = getopts('drnth', \%switches); +help() if(!$status || $switches{'h'}); +for (keys %switches) { + switch($_) { + case 'd' { $alive = 0; } + case 'r' { $alive = 1; } + case 'n' { $name = 1; } + case 't' { $time = 1; } + else {} } }