From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 915491F5A8 for ; Fri, 15 Nov 2019 09:51:01 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 05/29] init: pass global variables into subs Date: Fri, 15 Nov 2019 09:50:36 +0000 Message-Id: <20191115095100.25633-6-e@80x24.org> In-Reply-To: <20191115095100.25633-1-e@80x24.org> References: <20191115095100.25633-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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(-) 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;