Commit 9ba96fbb authored by Richard Levitte's avatar Richard Levitte
Browse files

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: default avatarRich Salz <rsalz@openssl.org>
parent c15e95a6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ my @directory_vars = ( "dir", "certs", "crl_dir", "new_certs_dir" );
my @file_vars = ( "database", "certificate", "serial", "crlnumber",
		  "crl", "private_key", "RANDFILE" );
while(<STDIN>) {
    chomp;
    s|\R$||;
    foreach my $d (@directory_vars) {
	if (/^(\s*\#?\s*${d}\s*=\s*)\.\/([^\s\#]*)([\s\#].*)$/) {
	    $_ = "$1sys\\\$disk:\[.$2$3";
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ my %translations = ();
open DEMANGLER_DATA, $ARGV[0]
    or die "Couldn't open $ARGV[0]: $!\n";
while(<DEMANGLER_DATA>) {
    chomp;
    s|\R$||;
    (my $translated, my $original) = split /\$/;
    $translations{$original} = $translated.'$';
}
+1 −1
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ if ($WHAT eq '-newcert' ) {
    # ask user for existing CA certificate
    print "CA certificate filename (or enter to create)\n";
    $FILE = <STDIN>;
    chop $FILE if $FILE;
    $FILE = s|\R$|| if $FILE;
    if ($FILE) {
        copy_pemfile($FILE,"${CATOP}/private/$CAKEY", "PRIVATE");
        copy_pemfile($FILE,"${CATOP}/$CACERT", "CERTIFICATE");
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
while (<>)
	{
	next unless /^node/;
	chop;
	s|\R$||;                # Better chomp
	@a=split;
	$num{$a[3]}++;
	}
+1 −1
Original line number Diff line number Diff line
@@ -257,7 +257,7 @@ foreach (@out)
				}
			$out=$t;
			}
		chop $out;
		chop $out;      # Get rid of the last comma
		print OUT "$out";
		}
	else
Loading