From 7427c871abee6768cdd1c12d0ddf23f13cb37197 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Mon, 15 Feb 2010 23:47:16 +0100 Subject: Various stylistic improvements suggested by Max (such as using the // op). --- emergencyc.pl | 20 ++++---------------- emergencyd.pl | 16 +++++++--------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/emergencyc.pl b/emergencyc.pl index a7d55d2..b8bebb6 100755 --- a/emergencyc.pl +++ b/emergencyc.pl @@ -46,18 +46,8 @@ if ( defined($opts{K}) ) { } die "No key specified (use -K or -k option)" unless defined($key); -my $host; -if ( defined($opts{h}) ) { - $host = $opts{h}; -} else { - $host = "localhost"; -} -my $port; -if ( defined($opts{p}) ) { - $port = $opts{p}; -} else { - $port = DEFAULT_PORT; -} +my $host = $opts{h} // "localhost"; +my $port = $opts{p} // DEFAULT_PORT; my @res = getaddrinfo($host, $port, AF_UNSPEC, SOCK_DGRAM) or die "Cannot resolve host $host port $port"; @@ -72,10 +62,8 @@ sub curtime { return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year+1900,$mon+1,$mday,$hour,$min,$sec); } -my $command = $ARGV[0]; -$command = "PING" unless defined($command); -my $timestamp = $opts{t}; -$timestamp = curtime unless defined($timestamp); +my $command = $ARGV[0] // "PING"; +my $timestamp = $opts{t} // curtime; my $validate = "$command|$timestamp"; my $maccheck = hmac_sha256_hex($validate, $key); send $socket, "$command|$timestamp|$maccheck", 0, $haddr; diff --git a/emergencyd.pl b/emergencyd.pl index 5766484..200881b 100755 --- a/emergencyd.pl +++ b/emergencyd.pl @@ -90,13 +90,12 @@ my $proto = getprotobyname("udp") or die "Can't resolve udp protocol: $!"; my $port; if ( defined($opts{p}) ) { $port = $opts{p}; - $port =~ /^(\d+)$/ or die "Invalid port number (-p option) $port"; + $port =~ /^\d+$/ or die "Invalid port number (-p option) $port"; } else { $port = DEFAULT_PORT; } -my $socket; -socket $socket, PF_INET6, SOCK_DGRAM, $proto or die "Can't create socket: $!"; +socket my $socket, PF_INET6, SOCK_DGRAM, $proto or die "Can't create socket: $!"; if ( defined(IPV6_V6ONLY) ) { setsockopt $socket, IPPROTO_IPV6, IPV6_V6ONLY, 0 or die "Can't set IPV6_V6ONLY option to 0: $!"; } @@ -114,7 +113,7 @@ if ( $opts{f} ) { } sub curtime { - my $fiddle = shift; $fiddle = 0 unless defined($fiddle); + my $fiddle = shift // 0; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time+$fiddle); return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year+1900,$mon+1,$mday,$hour,$min,$sec); } @@ -126,9 +125,9 @@ while (1) { my $buf; my $sender = recv($socket, $buf, 16384, 0); my @lines = split /\015*\012|\|/s, $buf; - my $command = $lines[0]; $command = "" unless defined($command); - my $timestamp = $lines[1]; $timestamp = "" unless defined($timestamp); - my $maccheck = $lines[2]; $maccheck = "" unless defined($maccheck); + my $command = $lines[0] // ""; + my $timestamp = $lines[1] // ""; + my $maccheck = $lines[2] // ""; next PACKET if $command eq ""; if ( $command eq "PING" ) { send $socket, "PONG\n", 0, $sender; @@ -157,8 +156,7 @@ while (1) { if ( $command eq "NOOP" ) { send $socket, "NOOP\n", 0, $sender; } elsif ( $command eq "DPID" ) { - my $pid = POSIX::getpid; - send $socket, "DPID\n$pid\n", 0, $sender; + send $socket, "DPID\n$$\n", 0, $sender; } elsif ( $command eq "DIE!" ) { send $socket, "BYE!\n", 0, $sender; exit 0; -- cgit v1.2.3