Skip to content

Commit

Permalink
paste: factor loop (#898)
Browse files Browse the repository at this point in the history
* The input loop was written as a forever loop with an eof check at the top of loop body
* Convert the eof check to a function so the loop can be written as a "while"
* Also add --version as done in other scripts
* Regression test: perl paste /etc/motd /etc/motd
  • Loading branch information
mknos authored Jan 3, 2025
1 parent 9555402 commit 9df8833
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions bin/paste
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ my $Program = basename($0);
my ($VERSION) = '1.4';
my (@fh, @sep, %opt);

sub usage {
print "usage: $Program [-s] [-d list] file ...\n";
exit EX_FAILURE;
}

getopts('d:s', \%opt) or usage();
@ARGV or usage();

Expand Down Expand Up @@ -72,18 +67,8 @@ if ($opt{'s'}) {
exit EX_SUCCESS;
}

my $files_still_open = 1;
INPUT: while (1) {
while (files_open()) {
my $current_sep = 0;
$files_still_open = 0;
CHECKEOF: for my $i (0 .. $#fh) {
unless (eof $fh[$i]) {
$files_still_open = 1;
last CHECKEOF;
}
}
last INPUT unless $files_still_open;

my $tline;
for my $i (0..$#fh) {
if (not eof $fh[$i]) {
Expand All @@ -101,6 +86,23 @@ INPUT: while (1) {
}
exit EX_SUCCESS;

sub files_open {
for my $f (@fh) {
return 1 unless eof $f;
}
return 0;
}

sub usage {
print "usage: $Program [-s] [-d list] file ...\n";
exit EX_FAILURE;
}

sub VERSION_MESSAGE {
print "$Program version $VERSION\n";
exit EX_SUCCESS;
}

__END__
=pod
Expand Down

0 comments on commit 9df8833

Please sign in to comment.