Use the "modulino" pattern to facilitate unit tests
Now the `ddclient` file can be used as a script or as a module. For details, see: https://www.drdobbs.com/scripts-as-modules/184416165 Addresses #147
This commit is contained in:
parent
8cbcecba99
commit
f6f920eb39
1 changed files with 101 additions and 88 deletions
|
@ -18,6 +18,7 @@
|
|||
#
|
||||
#
|
||||
######################################################################
|
||||
package ddclient;
|
||||
require v5.10.1;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
@ -25,7 +26,8 @@ use Getopt::Long;
|
|||
use Sys::Hostname;
|
||||
use IO::Socket;
|
||||
|
||||
my $version = "3.9.1";
|
||||
use version 0.77; our $VERSION = version->declare('v3.9.1');
|
||||
(my $version = $VERSION->stringify()) =~ s/^v//;
|
||||
my $programd = $0;
|
||||
$programd =~ s%^.*/%%;
|
||||
my $program = $programd;
|
||||
|
@ -64,6 +66,11 @@ local $lineno = '';
|
|||
|
||||
$ENV{'PATH'} = (exists($ENV{PATH}) ? "$ENV{PATH}:" : "") . "/sbin:/usr/sbin:/bin:/usr/bin:/etc:/usr/lib:";
|
||||
|
||||
my ($result, %config, %globals, %cache);
|
||||
my $saved_cache;
|
||||
my %saved_opt;
|
||||
my $daemon;
|
||||
|
||||
sub T_ANY { 'any' }
|
||||
sub T_STRING { 'string' }
|
||||
sub T_EMAIL { 'e-mail address' }
|
||||
|
@ -829,11 +836,11 @@ my @opt = (
|
|||
" project now maintained on https://github.com/ddclient/ddclient"
|
||||
);
|
||||
|
||||
sub main {
|
||||
## process args
|
||||
my $opt_usage = process_args(@opt);
|
||||
my ($result, %config, %globals, %cache);
|
||||
my $saved_cache = '';
|
||||
my %saved_opt = %opt;
|
||||
$saved_cache = '';
|
||||
%saved_opt = %opt;
|
||||
$result = 'OK';
|
||||
|
||||
test_geturl(opt('geturl')) if opt('geturl');
|
||||
|
@ -874,7 +881,6 @@ if (opt('foreground')) {
|
|||
}
|
||||
|
||||
umask 077;
|
||||
my $daemon;
|
||||
do {
|
||||
$now = time;
|
||||
$result = 'OK';
|
||||
|
@ -928,6 +934,7 @@ unlink_pid();
|
|||
sendmail();
|
||||
|
||||
exit($result);
|
||||
}
|
||||
|
||||
######################################################################
|
||||
## runpostscript
|
||||
|
@ -5240,6 +5247,12 @@ sub nic_cloudns_update {
|
|||
}
|
||||
}
|
||||
|
||||
# Execute main() if this file is run as a script or run via PAR (https://metacpan.org/pod/PAR),
|
||||
# otherwise do nothing. This "modulino" pattern makes it possible to import this file as a module
|
||||
# and test its functions directly; there's no need for test-only command-line arguments or stdout
|
||||
# parsing.
|
||||
__PACKAGE__->main() unless caller() && caller() ne 'PAR';
|
||||
|
||||
######################################################################
|
||||
# vim: ai et ts=4 sw=4 tw=78:
|
||||
|
||||
|
|
Loading…
Reference in a new issue