File Coverage

blib/lib/Getopt/Long/Parser.pm
Criterion Covered Total %
statement 41 45 91.1
branch 4 6 66.6
condition 1 3 33.3
subroutine 8 9 88.8
pod 3 4 75.0
total 57 67 85.0


line stmt bran cond sub pod time code
1             #! perl
2              
3             # Parser.pm -- Getopt::Long object-oriented interface
4             # Author : Johan Vromans
5             # Created On : Thu Nov 9 10:37:00 2023
6             # Last Modified On: Tue Jun 11 13:17:57 2024
7             # Update Count : 16
8             # Status : Released
9              
10 3     3   367641 use strict;
  3         7  
  3         174  
11 3     3   26 use warnings;
  3         7  
  3         333  
12              
13             package Getopt::Long::Parser;
14              
15             # Must match Getopt::Long::VERSION!
16             our $VERSION = 2.58;
17              
18             =head1 NAME
19              
20             Getopt::Long::Parser - Getopt::Long object-oriented interface
21              
22             =head1 SYNOPSIS
23              
24             use Getopt::Long::Parser;
25             my $p = Getopt::Long::Parser->new;
26             $p->configure( %options );
27             if ( $p->getoptions( @options ) ) { ... }
28             if ( $p->getoptionsfromarray( \@array, @options ) ) { ... }
29              
30             Configuration options can be passed to the constructor:
31              
32             my $p = Getopt::Long::Parser->new( config => [ %options ] );
33              
34             =head1 DESCRIPTION
35              
36             C is an object-oriented interface to
37             L. See its documentation for configuration and use.
38              
39             Note that C and C are not
40             object-oriented.
41             C emulates an object-oriented interface,
42             which should be okay for most purposes.
43              
44             =head1 CONSTRUCTOR
45              
46             my $p = Getopt::Long::Parser->new( %options );
47              
48             The constructor takes an optional hash with parameters.
49              
50             =over 4
51              
52             =item config
53              
54             An array reference with configuration settings.
55             See L for all possible settings.
56              
57             =back
58              
59             =cut
60              
61             # Getopt::Long has a stub for Getopt::Long::Parser::new.
62 3     3   1641 use Getopt::Long ();
  3         12  
  3         109  
63 3     3   18 no warnings 'redefine';
  3         6  
  3         854  
64              
65             sub new {
66 3     3 0 190829 my $that = shift;
67 3   33     18 my $class = ref($that) || $that;
68 3         7 my %atts = @_;
69              
70             # Register the callers package.
71 3         14 my $self = { caller_pkg => (caller)[0] };
72              
73 3         8 bless ($self, $class);
74              
75 3         8 my $default_config = Getopt::Long::_default_config();
76              
77             # Process config attributes.
78 3 100       10 if ( defined $atts{config} ) {
79 1         2 my $save = Getopt::Long::Configure ($default_config, @{$atts{config}});
  1         5  
80 1         2 $self->{settings} = Getopt::Long::Configure ($save);
81 1         3 delete ($atts{config});
82             }
83             # Else use default config.
84             else {
85 2         9 $self->{settings} = $default_config;
86             }
87              
88 3 50       9 if ( %atts ) { # Oops
89 0         0 die(__PACKAGE__.": unhandled attributes: ".
90             join(" ", sort(keys(%atts)))."\n");
91             }
92              
93 3         9 $self;
94             }
95              
96 3     3   24 use warnings 'redefine';
  3         6  
  3         958  
97              
98             =head1 METHODS
99              
100             In the examples, C<$p> is assumed to be the result of a call to the constructor.
101              
102             =head2 configure
103              
104             $p->configure( %settings );
105              
106             Update the current config settings.
107             See L for all possible settings.
108              
109             =cut
110              
111             sub configure {
112 0     0 1 0 my ($self) = shift;
113              
114             # Restore settings, merge new settings in.
115 0         0 my $save = Getopt::Long::Configure ($self->{settings}, @_);
116              
117             # Restore orig config and save the new config.
118 0         0 $self->{settings} = Getopt::Long::Configure ($save);
119             }
120              
121             =head2 getoptionsfromarray
122              
123             my $res = $p->getoptionsfromarray( $aref, @opts );
124              
125             =head2 getoptions
126              
127             my $res = $p->getoptions( @opts );
128              
129             The same as C.
130              
131             =cut
132              
133             sub getoptions {
134 1     1 1 28 my ($self) = shift;
135              
136 1         5 return $self->getoptionsfromarray(\@ARGV, @_);
137             }
138              
139             sub getoptionsfromarray {
140 2     2 1 5 my ($self) = shift;
141              
142             # Restore config settings.
143 2         4 my $save = Getopt::Long::Configure ($self->{settings});
144              
145             # Call main routine.
146 2         3 my $ret = 0;
147 2         9 $Getopt::Long::caller = $self->{caller_pkg};
148              
149 2         2 eval {
150             # Locally set exception handler to default, otherwise it will
151             # be called implicitly here, and again explicitly when we try
152             # to deliver the messages.
153 2         9 local ($SIG{__DIE__}) = 'DEFAULT';
154 2         5 $ret = Getopt::Long::GetOptionsFromArray (@_);
155             };
156              
157             # Restore saved settings.
158 2         5 Getopt::Long::Configure ($save);
159              
160             # Handle errors and return value.
161 2 50       2 die ($@) if $@;
162 2         7 return $ret;
163             }
164              
165             =head1 SEE ALSO
166              
167             L
168              
169             =head1 AUTHOR
170              
171             Johan Vromans
172              
173             =head1 COPYRIGHT AND DISCLAIMER
174              
175             This program is Copyright 1990,2015,2023 by Johan Vromans.
176             This program is free software; you can redistribute it and/or
177             modify it under the same terms as Perl.
178              
179             =cut
180              
181             1;