From ca783e786a2559ecd161c33be17e46d3baaa25be Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 15 Nov 2019 09:50:36 +0000 Subject: init: pass global variables into subs Avoid 'Variable "%s" will not stay shared' warnings when the contents of this script eval'ed into a sub. We also need to rely on ->DESTROY instead of END{} to unlink the lock file on sub exit. --- script/public-inbox-init | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'script/public-inbox-init') diff --git a/script/public-inbox-init b/script/public-inbox-init index 50711266..da683657 100755 --- a/script/public-inbox-init +++ b/script/public-inbox-init @@ -5,7 +5,12 @@ # Initializes a public-inbox, basically a wrapper for git-init(1) use strict; use warnings; -my $usage = "public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..]"; +sub usage { + print STDERR < $dir); my $lockfile = "$pi_config.lock"; my $lockfh; sysopen($lockfh, $lockfile, O_RDWR|O_CREAT|O_EXCL) or do { - $lockfh = undef; warn "could not open config file: $lockfile: $!\n"; exit(255); }; -END { unlink($lockfile) if $lockfh }; - +my $auto_unlink = UnlinkMe->new($lockfile); my $perm; if (-e $pi_config) { open(my $oh, '<', $pi_config) or die "unable to read $pi_config: $!\n"; @@ -166,3 +168,18 @@ if (defined $perm) { rename $pi_config_tmp, $pi_config or die "failed to rename `$pi_config_tmp' to `$pi_config': $!\n"; +$auto_unlink->DESTROY; + +package UnlinkMe; +use strict; + +sub new { + my ($klass, $file) = @_; + bless { file => $file }, $klass; +} + +sub DESTROY { + my $f = delete($_[0]->{file}); + unlink($f) if defined($f); +} +1; -- cgit v1.2.3-24-ge0c7