]> Sergey Matveev's repositories - public-inbox.git/commitdiff
init: pass global variables into subs
authorEric Wong <e@80x24.org>
Fri, 15 Nov 2019 09:50:36 +0000 (09:50 +0000)
committerEric Wong <e@80x24.org>
Sat, 16 Nov 2019 11:05:23 +0000 (11:05 +0000)
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

index 507112661ddc6a61619b7e30cfc2bbbca59cec71..da683657a0b1b38fd54a44dcc35af32dbcc677b6 100755 (executable)
@@ -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 <<EOF;
+Usage: public-inbox-init NAME INBOX_DIR HTTP_URL ADDRESS [ADDRESS..]
+EOF
+       exit 1;
+}
 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
 use PublicInbox::Admin;
 PublicInbox::Admin::require_or_die('-base');
@@ -19,7 +24,6 @@ use Fcntl qw(:DEFAULT);
 use Cwd qw/abs_path/;
 
 sub x { system(@_) and die join(' ', @_). " failed: $?\n" }
-sub usage { print STDERR "Usage: $usage\n"; exit 1 }
 my $version = undef;
 my $indexlevel = undef;
 my $skip_epoch;
@@ -57,12 +61,10 @@ my ($fh, $pi_config_tmp) = tempfile('pi-init-XXXXXXXX', DIR => $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;