Update a configuration file: Difference between revisions

Content added Content deleted
mNo edit summary
mNo edit summary
Line 149: Line 149:
=={{header|Perl}}==
=={{header|Perl}}==


<lang Perl>#!/usr/bin/perl
<lang Perl>use warnings;

use warnings;
use strict;
use strict;


Line 164: Line 162:
sub config {
sub config {
my (@config) = <DATA>;
my (@config) = <DATA>;
push @config, "NUMBEROFSTRAWBERRIES $numberofstrawberries\n" unless grep { /^;*[ \t]*NUMBEROFSTRAWBERRIES\b/; } @config;
push @config, "NUMBEROFSTRAWBERRIES $numberofstrawberries\n"
unless grep { /^;*[ \t]*NUMBEROFSTRAWBERRIES\b/; } @config;


foreach my $line (@config) {
foreach my $line (@config) {
next if $line =~ /^[;\t ]+$/;


if (substr($line, 0, 1) eq '#') {
if (substr($line, 0, 1) eq '#') {
Line 173: Line 171:
next;
next;
}
}

next if $line =~ /^[;\t ]+$/;


my ($option, $option_name);
my ($option, $option_name);
Line 189: Line 189:
}
}
}
}
elsif ($line =~ /^;\s*([A-Z]+[0-9]*)/) {
elsif ($line =~ /^;+\s*([A-Z]+[0-9]*)/) {
$option_name = lc $1;
$option_name = lc $1;
$option = eval "\\\$$option_name";
$option = eval "\\\$$option_name";
Line 204: Line 204:


if (defined $$option) {
if (defined $$option) {
push @out, "\U$option_name\E $$option\n" unless grep { /^\U$option_name\E\b/; } @out;
push @out, "\U$option_name\E $$option\n"
unless grep { /^\U$option_name\E\b/; } @out;
}
}
else {
else {