Anonymous user
Update a configuration file: Difference between revisions
add Perl 6 solution
(move Perl section to the correct place in the page) |
(add Perl 6 solution) |
||
Line 1,317:
# How many bananas we have
NUMBEROFBANANAS 48</lang>
=={{header|Perl 6}}==
Implemented as a command-line script which can make arbitrary in-place updates to such config files.
Assuming that the script is saved as <tt>conf-update</tt> and the config file as <tt>test.cfg</tt>, the four changes required by the task description could be performed with the command:
<pre>conf-update --/needspeeling --seedsremoved --numberofbananas=1024 --numberofstrawberries=62000 test.cfg</pre>
The script:
<lang perl6>#!/usr/bin/env perl6
my $tmpfile = tmpfile;
sub MAIN ($file, *%changes) {
%changes.=map({; .key.uc => .value });
my %seen;
my $out = open $tmpfile, :w;
for $file.IO.lines {
when /:s ^ ('#' .* | '') $/ {
say $out: ~$0;
}
when /:s ^ (';'+)? [(\w+) (\w+)?]? $/ {
next if !$1 or %seen{$1.uc}++;
my $new = %changes{$1.uc}:delete;
say $out: format-line $1, |($new ~~ Bool ?? ($2, $new) !! ($new//$2, True));
}
default {
note "Malformed line: $_\nAborting.";
exit 1;
}
}
say $out: format-line .key, |(.value ~~ Bool ?? (42, .value) !! (.value, 42))
for %changes;
run 'mv', $tmpfile, $file; # work-around for NYI `move $tmpfile, $file;`
}
END { unlink $tmpfile if $tmpfile.IO.e }</lang>
=={{header|PicoLisp}}==
|