blib/lib/Net/Curl/Easier.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Net::Curl::Easier;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Net::Curl::Easier - Convenience wrapper around L<Net::Curl::Easy>
11  
12 =head1 SYNOPSIS
13  
14 my $easy = Net::Curl::Easier->new( url => 'http://perl.org' )->perform();
15  
16 print $easy->body();
17  
18 # … or, to dig in to the response:
19 my $response = HTTP::Response->parse( $easy->head() . $easy->body() );
20  
21 =head1 DESCRIPTION
22  
23 L<Net::Curl> is wonderful, but L<Net::Curl::Easy> is a bit clunky for
24 day-to-day use. This library attempts to make that, well, “easier”. :-)
25  
26 This module extends Net::Curl::Easy, with differences as noted here:
27  
28 =head1 DIFFERENCES FROM Net::Curl::Easy
29  
30 =over
31  
32 =item * The response headers and body go to an internal buffer by default.
33 Net::Curl::Easy simply adopts libcurl’s defaults, which is understandable
34 but frequently unhelpful.
35  
36 =item * Character encoding. As of this writing Net::Curl::Easy uses
37 L<SvPV|https://perldoc.perl.org/perlapi#SvPV> to translate Perl strings to C,
38 which means that what libcurl receives depends on how Perl internally stores
39 your string. Thus, the same string given to Net::Curl::Easy can yield
40 different input to libcurl depending on how Perl has decided to store that
41 string.
42  
43 This library fixes that by requiring all strings that it receives
44 to be B<byte> B<strings> and normalizing Perl’s internal storage before
45 calling into Net::Curl::Easy.
46  
47 =item * Several methods are wrapped, as described below.
48  
49 =back
50  
51 =head1 SEE ALSO
52  
53 =over
54  
55 =item * L<Net::Curl::Promiser> wraps L<Net::Curl::Multi> with promises.
56 Recommended for concurrent queries!
57  
58 =item * L<Net::Curl::Simple> takes a similar approach to this module but
59 presents a substantially different interface.
60  
61 =back
62  
63 =cut
64  
65 #----------------------------------------------------------------------
66  
67 1 ▶use parent 'Net::Curl::Easy';
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 our $VERSION = '0.01';
70  
71 #----------------------------------------------------------------------
72  
73 =head1 METHODS
74  
75 Besides those inherited from Net::Curl::Easy, this class defines:
76  
77 =head2 $obj = I<OBJ>->set( $NAME1 => $VALUE1, $NAME2 => $VALUE2, .. )
78  
79 C<setopt()>s multiple values in a single call. Instead of:
80  
81 $easy->setopt( Net::Curl::Easy::CURLOPT_URL, 'http://perl.org' );
82 $easy->setopt( Net::Curl::Easy::CURLOPT_VERBOSE, 1 );
83  
84 … you can do:
85  
86 $easy->set( url => 'http://perl.org', verbose => 1 );
87  
88 See L<curl_easy_setopt(3)> for the full set of options you can give here.
89  
90 Note that, since I<OBJ> is returned, you can chain calls to this with
91 calls to other methods like C<perform()>.
92  
93 You may not need to call this method since C<new()> calls it for you.
94  
95 =cut
96  
97 sub set {
98 splice @_, 1, 0, 'setopt';
99 &_set_or_push;
100 }
101  
102 =head2 $obj = I<OBJ>->push( $NAME1 => \@VALUES1, $NAME2 => \@VALUES2, .. )
103  
104 Like C<set()>, but for C<pushopt()>.
105  
106 =cut
107  
108 sub push {
109 splice @_, 1, 0, 'pushopt';
110 &_set_or_push;
111 }
112  
113 sub _set_or_push {
114 my $self = shift;
115 my $method = shift;
116  
117 my @to_set;
118 while (my ($k, $v) = splice @_, 0, 2) {
119 $k =~ tr<a-z><A-Z>;
120  
121 $k = __PACKAGE__->can("CURLOPT_$k") || do {
122 require Carp;
123 Carp::croak("CURLOPT_$k doesn’t exist!");
124 };
125  
126 $k = $k->();
127  
128 CORE::push @to_set, [$k, $v];
129 }
130  
131 $self->$method(@$_) for @to_set;
132  
133 return $self;
134 }
135  
136 =head2 $value = I<OBJ>->get($NAME)
137  
138 Like C<set()>, but for C<getinfo()>. This, of course, doesn’t return
139 I<OBJ>, so it can’t be chained.
140  
141 =cut
142  
143 sub get {
144 my ($self, $name) = @_;
145  
146 $name =~ tr<a-z><A-Z>;
147  
148 my $nameval = __PACKAGE__->can("CURLINFO_$name") || do {
149 require Carp;
150 Carp::croak("CURLINFO_$name doesn’t exist!");
151 };
152  
153 return $self->getinfo($nameval->());
154 }
155  
156 =head2 $str = I<OBJ>->head()
157  
158 Returns I<OBJ>’s internal HTTP response header buffer, as a byte string.
159  
160 =cut
161  
162 sub head { $_[0]{'head'} }
163  
164 =head2 $str = I<OBJ>->body()
165  
166 Returns the HTTP response body, as a byte string.
167  
168 =cut
169  
170 sub body { $_[0]{'body'} }
171  
172 #----------------------------------------------------------------------
173  
174 =head1 WRAPPED METHODS
175  
176 =over
177  
178 =item * C<new()> takes a list of key/value pairs and passes it to
179 an internal call to C<set()>. The returned object will always be a
180 (newly-created) hash reference.
181  
182 =item * C<escape()> and C<send()> apply the character encoding fix described
183 above.
184  
185 =item * C<setopt()> and C<pushopt()> fix character encoding and return
186 the instance object.
187  
188 =item * C<perform()> returns the instance object.
189  
190 =back
191  
192 =cut
193  
194 sub escape {
195 utf8::downgrade($_[1]);
196  
197 return $_[0]->SUPER::escape($_[1]);
198 }
199  
200 sub send {
201 utf8::downgrade($_[1]);
202  
203 return $_[0]->SUPER::send($_[1]);
204 }
205  
206 sub setopt {
207 my ($self, $key, $value) = @_;
208  
209 utf8::downgrade($value) if !ref $value;
210  
211 $self->SUPER::setopt($key, $value);
212  
213 return $self;
214 }
215  
216 sub pushopt {
217 my ($self, $key, $values_ar) = @_;
218  
219 ref || utf8::downgrade($_) for @$values_ar;
220  
221 $self->SUPER::pushopt($key, $values_ar);
222  
223 return $self;
224 }
225  
226 sub perform {
227 my $self = shift;
228  
229 $self->SUPER::perform(@_);
230  
231 return $self;
232 }
233  
234 sub new {
235 my $class = shift;
236 my $self = $class->SUPER::new( { head => '', body => ''} );
237  
238 return $self->set(
239 file => \$self->{'body'},
240 headerdata => \$self->{'head'},
241 @_,
242 );
243 }
244  
245 =head1 STATIC FUNCTIONS
246  
247 For convenience, C<Net::Curl::Easy::strerror()> is aliased in this module.
248  
249 =cut
250  
251 *strerror = *Net::Curl::Easy::strerror;
252  
253 #----------------------------------------------------------------------
254  
255 =head1 LICENSE & COPYRIGHT
256  
257 Copyright 2021 by Gasper Software Consulting. All rights reserved.
258  
259 This library is licensed under the same terms as Perl itself.
260 See L<perlartistic> for details.
261  
262 =cut
263  
264 1;
↑ Summary