| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Util::CommandLine; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Command-line interface helper utility | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 1 |  |  | 1 |  | 197606 | use 5.008; | 
|  | 1 |  |  |  |  | 9 |  | 
| 5 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 16 |  | 
| 6 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 659 | use Getopt::Long 'GetOptions'; | 
|  | 1 |  |  |  |  | 8886 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 9 | 1 |  |  | 1 |  | 723 | use Pod::Usage 'pod2usage'; | 
|  | 1 |  |  |  |  | 32607 |  | 
|  | 1 |  |  |  |  | 93 |  | 
| 10 | 1 |  |  | 1 |  | 511 | use Proc::PID::File; | 
|  | 1 |  |  |  |  | 1861 |  | 
|  | 1 |  |  |  |  | 50 |  | 
| 11 | 1 |  |  | 1 |  | 6 | use Term::ReadKey 'ReadMode'; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 69 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = '1.07'; # VERSION | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 1 |  |  | 1 |  | 6 | use constant EXPORT_OK => [ qw( options pod2usage singleton readmode ) ]; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 129 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub import { | 
| 18 | 1 |  |  | 1 |  | 7 | my $self = shift; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | # determine the caller and setup the exports hash | 
| 21 | 1 |  |  |  |  | 2 | my $callpkg = caller(); | 
| 22 | 0 |  |  |  |  | 0 | my %exports = map { $_ => 1 } grep { | 
| 23 | 1 |  |  |  |  | 2 | my $x = $_; | 
|  | 0 |  |  |  |  | 0 |  | 
| 24 | 0 |  |  |  |  | 0 | grep { $_ eq $x } @{(EXPORT_OK)}; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 25 |  |  |  |  |  |  | } @_; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # method injection as appropriate | 
| 28 |  |  |  |  |  |  | { | 
| 29 | 1 |  |  | 1 |  | 7 | no strict 'refs'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 531 |  | 
|  | 1 |  |  |  |  | 2 |  | 
| 30 | 1 |  |  |  |  | 4 | *{"$callpkg\::$_"} = \&{"$self\::$_"} for ( keys %exports ); | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 1 | 50 |  |  |  | 3 | singleton() if ( grep { $_ eq 'singleton' } @_ ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 34 | 1 | 50 |  |  |  | 5 | options()   if ( grep { $_ eq 'podhelp'   } @_ ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 1 |  |  |  |  | 1613 | return; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub singleton { | 
| 40 |  |  |  |  |  |  | my @dirs = ( | 
| 41 |  |  |  |  |  |  | '/var/run', | 
| 42 |  |  |  |  |  |  | '/tmp', | 
| 43 |  |  |  |  |  |  | $ENV{HOME}, | 
| 44 | 0 |  |  | 0 | 1 |  | '.', | 
| 45 |  |  |  |  |  |  | '/', | 
| 46 |  |  |  |  |  |  | ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  |  | my $singleton; | 
| 49 | 0 |  | 0 |  |  |  | eval { | 
| 50 | 0 | 0 |  |  |  |  | if ( Proc::PID::File->running({ dir => shift @dirs }) ) { | 
| 51 | 0 |  |  |  |  |  | warn "Running as singleton; forcing exit of $0\n"; | 
| 52 | 0 |  |  |  |  |  | exit 1; | 
| 53 |  |  |  |  |  |  | } | 
| 54 | 0 |  |  |  |  |  | $singleton = 1; | 
| 55 |  |  |  |  |  |  | } while ( not $singleton and @dirs ); | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 0 | 0 |  |  |  |  | die "Unable to establish PID file for singleton functionality\n" unless ($singleton); | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 0 |  |  |  |  |  | return; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub options { | 
| 63 | 0 | 0 | 0 | 0 | 1 |  | shift if ( index( ( $_[0] || '' ), '::' ) != -1 ); | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 0 |  |  |  |  |  | my $settings = {}; | 
| 66 |  |  |  |  |  |  | GetOptions( | 
| 67 |  |  |  |  |  |  | map { | 
| 68 | 0 | 0 |  |  |  |  | if (/\{/) { | 
| 69 | 0 |  |  |  |  |  | $settings->{ ( split(/[|=]/) )[0] } = []; | 
| 70 | 0 |  |  |  |  |  | $_ => $settings->{ ( split(/[|=]/) )[0] }; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  | else { | 
| 73 | 0 |  |  |  |  |  | $_ => \$settings->{ ( split(/[|=]/) )[0] }; | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 0 | 0 |  |  |  |  | } map { split(/\s+/) } @_, qw( help man ) | 
|  | 0 |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | ) || pod2usage(0); | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 0 |  |  |  |  |  | for ( keys %$settings ) { | 
| 79 |  |  |  |  |  |  | delete $settings->{$_} if ( | 
| 80 |  |  |  |  |  |  | not defined $settings->{$_} or | 
| 81 |  |  |  |  |  |  | ( | 
| 82 |  |  |  |  |  |  | ref $settings->{$_} eq 'ARRAY' and ( | 
| 83 |  |  |  |  |  |  | not @{ $settings->{$_} } or | 
| 84 |  |  |  |  |  |  | ( | 
| 85 |  |  |  |  |  |  | @{ $settings->{$_} } == 1 and | 
| 86 | 0 | 0 | 0 |  |  |  | $settings->{$_}[0] eq '' | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 87 |  |  |  |  |  |  | ) | 
| 88 |  |  |  |  |  |  | ) | 
| 89 |  |  |  |  |  |  | ) | 
| 90 |  |  |  |  |  |  | ); | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 0 | 0 |  |  |  |  | pod2usage( '-exitstatus' => 1, '-verbose' => 1 ) if ( $settings->{'help'} ); | 
| 94 | 0 | 0 |  |  |  |  | pod2usage( '-exitstatus' => 0, '-verbose' => 2 ) if ( $settings->{'man'}  ); | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 0 |  |  |  |  |  | return $settings; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub readmode { | 
| 100 | 0 |  |  | 0 | 1 |  | return ReadMode(@_); | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __END__ |