Jump to content

Update a configuration file: Difference between revisions

→‎{{header|Perl 6}}: Update broken example. use a module for tempfile, explicitly close file
(Added Kotlin)
(→‎{{header|Perl 6}}: Update broken example. use a module for tempfile, explicitly close file)
Line 2,489:
The script:
 
<lang perl6>#!/usr/bin/envuse perl6File::Temp;
 
my ($tmpfile, $out) = tmpfiletempfile;
 
sub MAIN ($file, *%changes) {
%changes.=map({; .key.uc => .value });
my %seen;
 
my $out = open $tmpfile, :w;
for $file.IO.lines {
when /:s ^ ('#' .* | '') $/ {
Line 2,514 ⟶ 2,512:
}
}
 
say $out: format-line .key, |(.value ~~ Bool ?? (Nil, .value) !! (.value, True))
for %changes;
move $tmpfile, $file;
 
$out.close;
END { unlink $tmpfile if $tmpfile.IO.e }
 
my $out = opencopy $tmpfile, :w$file;
 
sub format-line ($key, $value, $enabled) {
("; " if !$enabled) ~ $key.uc ~ (" $value" if defined $value);
}
 
sub tmpfile {
$*SPEC.catfile: $*SPEC.tmpdir, ("a".."z").roll(20).join
}</lang>
 
10,343

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.