prefix.pl 475 B

123456789101112131415161718192021222324
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Prefix all lines with "# ", unbuffered. Command being piped in may need
  4. # to have unbuffering forced with "stdbuf -i0 -o0 -e0 $cmd".
  5. use strict;
  6. use IO::Handle;
  7. binmode STDIN;
  8. binmode STDOUT;
  9. STDOUT->autoflush(1);
  10. my $needed = 1;
  11. while (1) {
  12. my $char;
  13. my $bytes = sysread(STDIN, $char, 1);
  14. exit 0 if ($bytes == 0);
  15. if ($needed) {
  16. print "# ";
  17. $needed = 0;
  18. }
  19. print $char;
  20. $needed = 1 if ($char eq "\n");
  21. }