#!/usr/bin/env perl use strict; use warnings FATAL => 'all'; use Getopt::Std; use DBI; use POSIX; my $defaults = { a => 0, d => 'MariaDB', f => 0, h => 'localhost', p => 'testpassword', u => 'testuser', }; my $opts = { %$defaults }; # copy hash ref usage($defaults) unless getopts("ad:efh:p:u:", $opts); my $dsn = "DBI:$opts->{d}:;host=$opts->{h}"; print "connect to: $dsn\n"; my $dbh = DBI->connect($dsn, $opts->{u}, $opts->{p}, {AutoInactiveDestroy => $opts->{a}}); if ($opts->{f}) { my $childpid = fork(); die "fork failed: $!" unless defined($childpid); unless ($childpid) { # in child process if ($opts->{e}) { print "child $$ : initiating POSIX::_exit\n"; POSIX::_exit(0); } else { print "child $$ : initiating normal exit\n"; exit 0; } } waitpid($childpid, 0); print "parent $$ : child process complete\n"; } $dbh->do('select 1'); print "parent $$ : exit\n"; exit 0; sub usage { my $defaults = shift; print STDERR <{d}) -e use POSIX::_exit instead of a normal exit -f fork a child -h server host IP or name (default: $defaults->{h}) -u database username (default: $defaults->{u}) -p database password (default: $defaults->{p}) EOF exit 1; }