Leser: 31
Question: welchen Programmierstil findet ihr lesbarer? | |
12 voter(s) so far |
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 55 56 57 58 59 60 61 62 63 64 65
use strict; use warnings; sub usage () { print STDERR "Usage $0 <unix|linux|dos|mac|apple>\n"; exit(8); } binmode(STDIN); binmode(STDOUT); my $eol = "\n"; if ($#ARGV != 0){ usage(); } if ($ARGV[0] eq 'linux') { $eol = "\n"; } elsif ($ARGV[0] eq 'unix') { $eol = "\n"; } elsif ($ARGV[0] eq 'dos') { $eol = "\r\n"; } elsif ($ARGV[0] eq 'apple') { $eol = "\r"; } elsif ($ARGV[0] eq 'mac') { $eol = "\r"; } else { usage(); } while (1) { my $ch; # Character from input # Read a character my $status = sysread(STDIN, $ch, 1); if ($status <= 0){ last; } if ($ch eq "\n"){ syswrite(STDOUT, $eol); next; } if ($ch eq "\r"){ my $next_ch; # Check for \r\n $status = sysread(STDIN, $next_ch, 1); if ($status <= 0){ syswrite(STDOUT, $eol); last; } # Check for \r\n if ($next_ch eq "\n"){ syswrite(STDOUT, $eol); next; } syswrite(STDOUT, $eol); $ch = $next_ch; } syswrite(STDOUT, $ch); }
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
use feature ":5.12"; sub usage { die "Usage $0 <unix|linux|dos|win|mac|apple>\n" } usage() if $#ARGV != 0; my $eol; given ($ARGV[0]){ when ([qw(unix linux)]){ $eol = "\n" } when ([qw(dos win)]) { $eol = "\r\n" } when ([qw(apple mac)]) { $eol = "\r" } default { usage() } } binmode(STDIN); binmode(STDOUT); my $last; while (sysread(STDIN, my $ch, 1) > 0) { if ($ch eq "\r" or $ch eq "\n"){ syswrite(STDOUT, $eol) unless $last eq "\r" and $ch eq "\n"; } else { syswrite(STDOUT, $ch) } $last = $ch; }
2010-08-10T19:33:03 reneeSo 100%ig vergleichen kann man das ja nicht, weil zu Zeiten als der Autor das Buch geschrieben hat die coolen Features aus 5.10 nicht da waren.
2010-08-10T19:33:03 reneeDie Subroutine usage finde ich aber in der Buchversion besser, weil ein "die" immer ein "at line..." mit sich bringt und das finde ich bei so einer Meldung eher hinderlich.
1 2 3 4 5 6
while (sysread(STDIN, my $ch, 1) > 0) { if ($ch eq "\r" or $ch eq "\n") {syswrite(STDOUT, $eol) unless $last eq "\r" and $ch eq "\n"} else {syswrite(STDOUT, $ch) } $last = $ch; }
1 2 3 4 5 6 7
my $eol = { linux => "\n", unix => "\n", dos => "\r\n", apple => "\r", mac => "\r", }->{ $ARGV[0] } or usage();
1 2 3 4
unless $last eq "\r" and $ch eq "\n"; if !($last eq "\r" and $ch eq "\n"); if !($last eq "\r") or !($ch eq "\n"); if $last ne "\r" or $ch ne "\n"
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 55 56 57 58 59 60 61 62 63 64 65
use strict; use warnings; my %eols=( unix => "\x0A", linux => "\x0A", dos => "\x0D\x0A", win => "\x0D\x0A", mac => "\x0D", apple => "\x0D", ); sub usage () { print STDERR "Usage $0 ".join('|', sort keys %eols)."\n"; exit(8); } sub determin_eol($$) { my ($last,$ch)=@_; my $write=''; # Win Ending if($last eq "\x0D" && $ch eq "\x0A") { $write=$eol; $last=''; } # Mac Ending elsif($last eq "\x0D") { $write=$eol; $last=$ch; } # UNX Ending elsif($last eq "\x0A") { $write=$eol; $last=$ch; } # no ending found else { $write=$last; $last=$ch; } return ($write,$last); } binmode(STDIN); binmode(STDOUT); usage() unless (@ARGV && $eols{$ARGV[0]}); my $eol=$eols{$ARGV[0]}; my $ch; my $last=''; while (sysread(STDIN, $ch, 1) > 0) { my $write=''; ($write,$last)=determin_eol($last,$ch); syswrite(STDOUT, $write); } # write last char. syswrite(STDOUT, join('',determin_eol($last,'')));
1 2 3 4
unless $last eq "\r" and $ch eq "\n"; if !($last eq "\r" and $ch eq "\n"); if !($last eq "\r") or !($ch eq "\n"); if $last ne "\r" or $ch ne "\n"
1 2 3 4 5 6
unix => "\x0A", linux => "\x0A", dos => "\x0D\x0A", win => "\x0D\x0A", mac => "\x0D", apple => "\x0D",