From 9ba96fbb2523cb12747c559c704c58bd8f9e7982 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Thu, 11 Feb 2016 21:47:30 +0100 Subject: Perl's chop / chomp considered bad, use a regexp instead Once upon a time, there was chop, which somply chopped off the last character of $_ or a given variable, and it was used to take off the EOL character (\n) of strings. ... but then, you had to check for the presence of such character. So came chomp, the better chop which checks for \n before chopping it off. And this worked well, as long as Perl made internally sure that all EOLs were converted to \n. These days, though, there seems to be a mixture of perls, so lines from files in the "wrong" environment might have \r\n as EOL, or just \r (Mac OS, unless I'm misinformed). So it's time we went for the more generic variant and use s|\R$||, the better chomp which recognises all kinds of known EOLs and chops them off. A few chops were left alone, as they are use as surgical tools to remove one last slash or one last comma. NOTE: \R came with perl 5.10.0. It means that from now on, our scripts will fail with any older version. Reviewed-by: Rich Salz --- crypto/lhash/num.pl | 2 +- crypto/objects/obj_dat.pl | 2 +- crypto/objects/objects.pl | 4 ++-- crypto/objects/objxref.pl | 6 +++--- crypto/perlasm/x86_64-xlate.pl | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) (limited to 'crypto') diff --git a/crypto/lhash/num.pl b/crypto/lhash/num.pl index 30fedf9cd5..4440a992dc 100644 --- a/crypto/lhash/num.pl +++ b/crypto/lhash/num.pl @@ -5,7 +5,7 @@ while (<>) { next unless /^node/; - chop; + s|\R$||; # Better chomp @a=split; $num{$a[3]}++; } diff --git a/crypto/objects/obj_dat.pl b/crypto/objects/obj_dat.pl index d726f2cb76..0bf1e4878f 100644 --- a/crypto/objects/obj_dat.pl +++ b/crypto/objects/obj_dat.pl @@ -257,7 +257,7 @@ foreach (@out) } $out=$t; } - chop $out; + chop $out; # Get rid of the last comma print OUT "$out"; } else diff --git a/crypto/objects/objects.pl b/crypto/objects/objects.pl index ea2caf5ec2..107647adbc 100644 --- a/crypto/objects/objects.pl +++ b/crypto/objects/objects.pl @@ -5,7 +5,7 @@ $max_nid=0; $o=0; while() { - chop; + s|\R$||; $o++; s/#.*$//; next if /^\s*$/; @@ -28,7 +28,7 @@ $Cname=""; $o=0; while () { - chop; + s|\R$||; $o++; if (/^!module\s+(.*)$/) { diff --git a/crypto/objects/objxref.pl b/crypto/objects/objxref.pl index 05b987ad16..7ebd74cdcc 100644 --- a/crypto/objects/objxref.pl +++ b/crypto/objects/objxref.pl @@ -13,7 +13,7 @@ open(IN, $mac_file) || die "Can't open $mac_file, $!\n"; while () { - chomp; + s|\R$||; # Better chomp my ($name, $num) = /^(\S+)\s+(\S+)$/; $oid_tbl{$name} = $num; } @@ -25,7 +25,7 @@ my $ln = 1; while () { - chomp; + s|\R$||; # Better chomp s/#.*$//; next if (/^\S*$/); my ($xr, $p1, $p2) = /^(\S+)\s+(\S+)\s+(\S+)/; @@ -112,6 +112,6 @@ sub check_oid my ($chk) = @_; if (!exists $oid_tbl{$chk}) { - die "Can't find \"$chk\", $!\n"; + die "Can't find \"$chk\"\n"; } } diff --git a/crypto/perlasm/x86_64-xlate.pl b/crypto/perlasm/x86_64-xlate.pl index 1f5bced8e1..a0b3bc0670 100755 --- a/crypto/perlasm/x86_64-xlate.pl +++ b/crypto/perlasm/x86_64-xlate.pl @@ -80,7 +80,7 @@ my $nasm=0; if ($flavour eq "mingw64") { $gas=1; $elf=0; $win64=1; $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`; - chomp($prefix); + $prefix =~ s|\R$||; # Better chomp } elsif ($flavour eq "macosx") { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; } elsif ($flavour eq "masm") { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; } @@ -852,7 +852,7 @@ ___ } while($line=<>) { - chomp($line); + $line =~ s|\R$||; # Better chomp $line =~ s|[#!].*$||; # get rid of asm-style comments... $line =~ s|/\*.*\*/||; # ... and C-style comments... -- cgit v1.2.3