@@ -23,10 +23,32 @@ BEGIN {
2323sub new {
2424 my $class = shift ;
2525 my $format = shift // ' Atom' ;
26+
27+ # Handle optional args hash
28+ my %args ;
29+ if (@_ && ref ($_ [-1]) eq ' HASH' && exists $_ [-1]-> {useragent }) {
30+ %args = %{pop @_ };
31+ # Validate useragent parameter
32+ if (exists $args {useragent }) {
33+ unless (blessed($args {useragent }) && $args {useragent }-> isa(' LWP::UserAgent' )) {
34+ Carp::croak(" useragent must be an LWP::UserAgent object" );
35+ }
36+ }
37+ # Only useragent is allowed for now
38+ my @invalid_keys = grep { $_ ne ' useragent' } keys %args ;
39+ if (@invalid_keys ) {
40+ Carp::croak(" Invalid argument(s): " . join (' , ' , @invalid_keys ));
41+ }
42+ }
43+
2644 my $format_class = ' XML::Feed::Format::' . $format ;
2745 eval " use $format_class " ;
2846 Carp::croak(" Unsupported format $format : $@ " ) if $@ ;
2947 my $feed = bless {}, join (' ::' , __PACKAGE__ , " Format" , $format );
48+
49+ # Store useragent if provided
50+ $feed -> {useragent } = $args {useragent } if exists $args {useragent };
51+
3052 $feed -> init_empty(@_ ) or return $class -> error($feed -> errstr);
3153 $feed ;
3254}
@@ -40,13 +62,13 @@ sub parse {
4062 my $feed = bless {}, $class ;
4163 my $xml = ' ' ;
4264 if (blessed($stream ) and $stream -> isa(' URI' )) {
43- $xml = $class -> get_uri($stream );
65+ $xml = $feed -> get_uri($stream );
4466 } elsif (ref ($stream ) eq ' SCALAR' ) {
4567 $xml = $$stream ;
4668 } elsif (ref ($stream )) {
47- $xml = $class -> get_fh($stream );
69+ $xml = $feed -> get_fh($stream );
4870 } else {
49- $xml = $class -> get_file($stream );
71+ $xml = $feed -> get_file($stream );
5072 }
5173 my $errstr = " Can't get feed XML content from $stream " ;
5274 if ($class -> errstr) {
@@ -97,9 +119,16 @@ sub get_uri {
97119 my $class = shift ;
98120 my ($stream ) = @_ ;
99121
100- my $ua = LWP::UserAgent-> new;
101- $ua -> agent(__PACKAGE__ . " /$VERSION " );
102- $ua -> env_proxy; # force allowing of proxies
122+ # Get or create UserAgent
123+ my $ua ;
124+ if (blessed($class ) && $class -> {useragent }) {
125+ $ua = $class -> {useragent };
126+ } else {
127+ $ua = LWP::UserAgent-> new;
128+ $ua -> agent(__PACKAGE__ . " /$VERSION " );
129+ $ua -> env_proxy; # force allowing of proxies
130+ }
131+
103132 my $res = URI::Fetch-> fetch($stream , UserAgent => $ua )
104133 or return $class -> error(URI::Fetch-> errstr);
105134 return $class -> error(" This feed has been permanently removed" )
@@ -180,6 +209,27 @@ sub _convert_entry {
180209 return $entry -> convert($feed_format );
181210}
182211
212+ sub useragent {
213+ my $feed = shift ;
214+ if (@_ ) {
215+ # Setter: validate the useragent
216+ my $ua = shift ;
217+ unless (blessed($ua ) && $ua -> isa(' LWP::UserAgent' )) {
218+ Carp::croak(" useragent must be an LWP::UserAgent object" );
219+ }
220+ $feed -> {useragent } = $ua ;
221+ return $ua ;
222+ } else {
223+ # Getter: return existing or create new one
224+ unless ($feed -> {useragent }) {
225+ $feed -> {useragent } = LWP::UserAgent-> new;
226+ $feed -> {useragent }-> agent(__PACKAGE__ . " /$VERSION " );
227+ $feed -> {useragent }-> env_proxy;
228+ }
229+ return $feed -> {useragent };
230+ }
231+ }
232+
183233sub base ;
184234sub format ;
185235sub title ;
@@ -262,12 +312,31 @@ L<DateTime> objects, which it then returns to the caller.
262312
263313=head2 XML::Feed->new($format)
264314
315+ =head2 XML::Feed->new($format, \%args)
316+
265317Creates a new empty I<XML::Feed > object using the format I<$format > .
266318
267319 $feed = XML::Feed->new('Atom');
268320 $feed = XML::Feed->new('RSS');
269321 $feed = XML::Feed->new('RSS', version => '0.91');
270322
323+ An optional hashref of arguments can be provided as the last parameter:
324+
325+ my $ua = LWP::UserAgent->new;
326+ $ua->timeout(30);
327+ $feed = XML::Feed->new('Atom', { useragent => $ua });
328+
329+ Currently supported arguments:
330+
331+ =over 4
332+
333+ =item * useragent
334+
335+ An L<LWP::UserAgent> object (or subclass) to use for fetching feeds via URI.
336+ If not provided, a default UserAgent will be created when needed.
337+
338+ =back
339+
271340=head2 XML::Feed->parse($stream)
272341
273342=head2 XML::Feed->parse($stream, $format)
@@ -405,6 +474,23 @@ object in the correct format for the feed.
405474Returns an XML representation of the feed, in the format determined by
406475the current format of the I<$feed > object.
407476
477+ =head2 $feed->useragent([ $ua ])
478+
479+ Get or set the L<LWP::UserAgent> object used for fetching feeds.
480+
481+ If called without arguments, returns the current UserAgent object,
482+ creating a default one if it doesn't exist.
483+
484+ If called with an L<LWP::UserAgent> object (or subclass), sets it as
485+ the UserAgent for this feed.
486+
487+ my $ua = LWP::UserAgent->new;
488+ $ua->timeout(30);
489+ $feed->useragent($ua);
490+
491+ # Later, retrieve it
492+ my $current_ua = $feed->useragent;
493+
408494=head2 $feed->first_link ([ $uri ])
409495
410496The Atom First-link for feed paging and archiving (RFC 5005).
0 commit comments