diff options
author | Nicholas Clark <nick@ccl4.org> | 2011-01-23 10:07:52 +0000 |
---|---|---|
committer | Nicholas Clark <nick@ccl4.org> | 2011-01-23 10:07:52 +0000 |
commit | f038801aea0ff24cf86511fa6679d7dcb859cd8d (patch) | |
tree | 8f46ea92b16a9121bab4ee0da59cdf219e43a254 /regen/regen_lib.pl | |
parent | 396ce246b9969d83ec11500def9604b58fb4c726 (diff) | |
download | perl-f038801aea0ff24cf86511fa6679d7dcb859cd8d.tar.gz |
In regen/*.pl, refactor the repeated code for close and rename if different.
Pass the final file name as an optional second argument of safer_open() and
store it with the file handle. Add a function close_and_rename() which closes
the file handle, then retrieves the final name, and renames the temporary file
if the two differ.
Diffstat (limited to 'regen/regen_lib.pl')
-rw-r--r-- | regen/regen_lib.pl | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/regen/regen_lib.pl b/regen/regen_lib.pl index 880a9754d5..d8cbd12bf5 100644 --- a/regen/regen_lib.pl +++ b/regen/regen_lib.pl @@ -62,13 +62,14 @@ sub rename_if_different { # Saf*er*, but not totally safe. And assumes always open for output. sub safer_open { - my $name = shift; + my ($name, $final_name) = @_; if (-f $name) { unlink $name or die "$name exists but can't unlink: $!"; } my $fh = gensym; open $fh, ">$name" or die "Can't create $name: $!"; *{$fh}->{name} = $name; + *{$fh}->{final_name} = $final_name if defined $final_name; binmode $fh; $fh; } @@ -128,4 +129,13 @@ EOM return $cooked; } +sub close_and_rename { + my $fh = shift; + my $name = *{$fh}->{name}; + die "No final name specified at open time for $name" + unless *{$fh}->{final_name}; + safer_close($fh); + rename_if_different($name, *{$fh}->{final_name}); +} + 1; |