-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping
executable file
·55 lines (46 loc) · 1.04 KB
/
ping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env perl
# ping - the pong player. The pong script has to have autoflushing on.
# Copyright (c) 2008 Vsevolod Kozlov <[email protected]>
# It's in public domain. What did you expect?
use strict;
use warnings;
use IPC::Open2;
use Time::HiRes qw|sleep|;
use Getopt::Std;
$| = 1;
my %opts;
getopts('vd:', \%opts);
my $pong = shift // './pong';
my ($pong_in, $pong_out, $input, $reversed);
my $count = 0;
my $pid = open2($pong_out, $pong_in, $pong);
print "Pong running - PID $pid\n\n";
$SIG{INT} = $SIG{TERM} = sub { print "\e[`Final count: $count\n" };
while ($input = <$pong_out>)
{
chomp $input;
if ($input =~ m/^|/)
{
++$count;
$reversed = reverse($input);
print $pong_in $reversed, "\n";
# If we've no -v, just print the count.
unless ($opts{v})
{
print "\e[`$count";
}
# If we have a delay, sleep it now.
if ($opts{d})
{
sleep $opts{d};
}
# If we're given -v, print some more prettiness.
if ($opts{v}) {
print $input, "\n";
print $reversed, "\n";
}
} else {
print "$input\n";
exit 1;
}
}