Skip to content

Instantly share code, notes, and snippets.

@moritz
Created February 7, 2012 09:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moritz/1758788 to your computer and use it in GitHub Desktop.
Save moritz/1758788 to your computer and use it in GitHub Desktop.
In-place file editing in Perl 6
sub inplace($filename, &c) {
my $f = open $filename;
# TODO: avoid collision of file names
my $tmp = "$filename.tmp";
try {
my @r := $f.lines.map: &c;
my $tmp-f = open :w, $tmp;
$tmp-f.say: $_ for @r;
$f.close;
$tmp-f.close;
unlink $f;
# TODO: proper escaping
shell("mv '$tmp' '$filename'");
CATCH {
unlink $tmp;
}
}
}
inplace('README', *.uc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment