Feed.pm

filter: Statementstmt 79.2% 157 / 198 Branchbran 50.0% 40 / 80 Conditioncond 45.0% 9 / 20 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 84.8% 28 / 33 Podpod 92.3% 12 / 13 totaltotal 70.0% 250 / 357 CC 54 SCAR 25.3
CC
54
Cov
69%
_parse_args
56.0
useragent
30.0
parse
21.5
CRAP 12.6
« Feed/Entry/Format/RSS.pm ↑ Summary Feed/Util.pm »
1 package XML::Feed;
2 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 35 ▶use base qw( Class::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use Feed::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use URI::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Module::Pluggable search_path => "XML::Feed::Format",
Subroutine BEGIN: called CC=1 SCAR=0.0
13 require => 1,
14 35 sub_name => 'formatters';
15  
16 35 our $VERSION = '1.0.1';
17 35 our $MULTIPLE_ENCLOSURES = 0;
18 35 our @formatters;
19 35 our %formatters;
20 35 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
21 35 @formatters = __PACKAGE__->formatters;
22 35 for my $formatter (@formatters) {
23 70 (my $name = $formatter) =~ s/^XML::Feed::Format:://;
24 70 $formatters{$name} = 1;
25 }
26 }
27  
28 0 ▶sub _parse_args {
Subroutine _parse_args: not called CC=7 SCAR=40.3
29 0 my $class = shift;
30 0 my ($possible_args) = @_;
31  
32 0 my @valid_args = qw[useragent];
33 0 my $args = {};
34  
35 0 for (@valid_args) {
36 $args->{$_} = delete $possible_args->{$_}
37 0 ▶ if exists $possible_args->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $possible_args->{$_} 0 0 0
38 }
39  
40 0 ▶ unless (keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$args) 0 0 0
41 0 Carp::croak('No valid keys passed to ' . __PACKAGE__ . 'new()');
42 }
43  
44 # Validate useragent parameter if provided
45 0 ▶ if (exists $args->{useragent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"useragent"}) 0 0 0
46 0 ▶ unless (blessed($args->{useragent}) && $args->{useragent}->isa('LWP::UserAgent')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($args->{"useragent"}) and $args->{"useragent"}->isa("LWP::UserAgent")) 0 0 0
Condition0%0/3
builtin::blessed($args->{"useragent"}) and $args->{"useragent"}->isa("LWP::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($args->{"useragent"}) and $args->{"useragent"}->isa("LWP::UserAgent")
builtin::blessed($args->{"useragent"}) $args->{"useragent"}->isa("LWP::UserAgent")
Truth table0%0/3
A and B
A= builtin::blessed($args->{"useragent"})B= $args->{"useragent"}->isa("LWP::UserAgent")
ABresult
0X0
100
111
47 0 Carp::croak("useragent must be an LWP::UserAgent object");
48 }
49 }
50  
51 0 return $args;
52 }
53  
54 14 ▶sub new {
Subroutine new: called CC=7 SCAR=20.4
Pod documented
55 14 my $class = shift;
56 14 ▶ my $format = shift // 'Atom';
Condition100%2/2
shift() // "Atom"
l!l
131
MC/DC100%1/1
shift() // "Atom"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
57
58 # Handle optional args hash
59 14 my $args = {};
60  
61 14 ▶ if (@_ && ref($_[-1]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[-1] eq "HASH") 0 14 14
Condition33%1/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
1400
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
62 0 $args = $class->_parse_args(pop @_);
63 }
64
65 Carp::croak("Unsupported format $format: not a recognised feed format")
66 14 ▶ unless $formatters{$format};
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::Feed::formatters{$format} 1 13 14
67 13 my $format_class = 'XML::Feed::Format::' . $format;
68 13 eval "use $format_class";
69 13 ▶ Carp::croak("Unsupported format $format: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
70 13 my $feed = bless $args, join('::', __PACKAGE__, "Format", $format);
71
72 13 ▶ $feed->init_empty(@_) or return $class->error($feed->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$feed->init_empty(@_) or return $class->error($feed->errstr) 7 0 7
73 7 $feed;
74 }
75  
76 0 ▶sub init_empty { 1 }
Subroutine init_empty: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 36 ▶sub parse {
Subroutine parse: called CC=17 SCAR=30.7
Pod documented
79 36 my $class = shift;
80 36 my($stream) = @_;
81 36 ▶ return $class->error("Stream parameter is required") unless $stream;
Branch: 1 branch
BranchTrueFalseTotal
unless $stream 0 36 36
82  
83 36 my ($specified_format, $args) = (undef, {});
84  
85 36 ▶ if (@_ == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 3) { } 0 36 36
elsif (@_ == 2) { } 0 36 36
86 0 ($specified_format, $args) = @_[1, 2];
87 } elsif (@_ == 2) {
88 0 ▶ if (ref($_[-1]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] eq 'HASH') { } 0 0 0
89 0 $args = $_[1];
90 } else {
91 0 $specified_format = $_[1];
92 }
93 }
94  
95 36 ▶ if (keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$args) 0 36 36
96 0 $args = $class->_parse_args($args);
97 }
98  
99 36 my $feed = bless $args, $class;
100 36 my $xml = '';
101 36 ▶ if (blessed($stream) and $stream->isa('URI')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($stream) and $stream->isa('URI')) { } 1 35 36
elsif (ref $stream eq 'SCALAR') { } 8 27 35
elsif (ref $stream) { } 1 26 27
Condition66%2/3
builtin::blessed($stream) and $stream->isa("URI")
!ll&&!rl&&r
3501
MC/DC50%1/2
builtin::blessed($stream) and $stream->isa("URI")
builtin::blessed($stream) $stream->isa("URI")
Truth table66%2/3
A and B
A= builtin::blessed($stream)B= $stream->isa("URI")
ABresult
0X0
100
111
102 1 $xml = $feed->get_uri($stream);
103 } elsif (ref($stream) eq 'SCALAR') {
104 8 $xml = $$stream;
105 } elsif (ref($stream)) {
106 1 $xml = $feed->get_fh($stream);
107 } else {
108 26 $xml = $feed->get_file($stream);
109 }
110 36 my $errstr = "Can't get feed XML content from $stream";
111 36 ▶ if ($class->errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->errstr) 0 36 36
112 0 $errstr .= ": " . $class->errstr;
113 }
114 36 ▶ return $class->error($errstr)
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 36 36
115 unless $xml;
116 36 my $format;
117 36 ▶ if ($specified_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($specified_format) { } 0 36 36
118 0 $format = $specified_format;
119 } else {
120 36 ▶ $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$format = $feed->identify_format(\$xml) or return $class->error($feed->errstr) 36 0 36
121 }
122  
123 36 my $format_class = join '::', __PACKAGE__, "Format", $format;
124 return $class->error("Unsupported format $format: not a recognised feed format")
125 36 ▶ unless $formatters{$format};
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::Feed::formatters{$format} 0 36 36
126 36 ▶ eval "use $format_class";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
127 36 ▶ return $class->error("Unsupported format $format: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 36 36
128 36 bless $feed, $format_class;
129 36 ▶ $feed->init_string(\$xml) or return $class->error($feed->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$feed->init_string(\$xml) or return $class->error($feed->errstr) 18 0 18
130 18 $feed;
131 }
132  
133 26 ▶sub get_file {
Subroutine get_file: called CC=2 SCAR=7.0
Pod documented
134 26 my $class = shift;
135 26 my ($filename) = @_;
136  
137 26 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or return $class->error("Can't open ${filename}: $!") 26 0 26
138 or return $class->error("Can't open $filename: $!");
139 26 my $xml = $class->get_fh($fh);
140 26 close $fh;
141  
142 26 return $xml;
143 }
144  
145 27 ▶sub get_fh {
Subroutine get_fh: called CC=2 SCAR=6.9
Pod documented
146 27 my $class = shift;
147 27 my ($fh) = @_;
148  
149 27 my $xml;
150 27 ▶ while (read $fh, my($chunk), 8192) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $chunk, 8192) 28 27 55
151 28 $xml .= $chunk;
152 }
153  
154 27 return $xml;
155 }
156  
157 1 ▶sub get_uri {
Subroutine get_uri: called CC=5 SCAR=17.4
Pod documented
158 1 my $class = shift;
159 1 my ($stream) = @_;
160  
161 # Get or create UserAgent
162 1 my $ua;
163 1 ▶ if (blessed($class) && $class->{useragent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class) and $class->{'useragent'}) { } 0 1 1
Condition33%1/3
builtin::blessed($class) and $class->{"useragent"}
!ll&&!rl&&r
010
MC/DC0%0/2
builtin::blessed($class) and $class->{"useragent"}
builtin::blessed($class) $class->{"useragent"}
Truth table33%1/3
A and B
A= builtin::blessed($class)B= $class->{"useragent"}
ABresult
0X0
100
111
164 0 $ua = $class->{useragent};
165 } else {
166 1 $ua = LWP::UserAgent->new;
167 1 $ua->agent(__PACKAGE__ . "/$VERSION");
168 1 $ua->env_proxy; # force allowing of proxies
169 }
170
171 1 ▶ my $res = URI::Fetch->fetch($stream, UserAgent => $ua)
Branch: 1 branch
BranchTrueFalseTotal
my $res = "URI::Fetch"->fetch($stream, "UserAgent", $ua) or return $class->error("URI::Fetch"->errstr) 1 0 1
172 or return $class->error(URI::Fetch->errstr);
173 1 ▶ return $class->error("This feed has been permanently removed")
Branch: 1 branch
BranchTrueFalseTotal
if $res->status == 410 0 1 1
174 if $res->status == URI::Fetch::URI_GONE();
175 1 return $res->content;
176 }
177  
178 36 ▶sub identify_format {
Subroutine identify_format: called CC=4 SCAR=14.0
Pod documented
179 36 my $feed = shift;
180 36 my($xml) = @_;
181 36 foreach my $class (@formatters) {
182 54 my ($name) = ($class =~ m!([^:]+)$!);
183 # TODO ugly
184 54 my $tmp = $$xml;
185 54 ▶ return $name if eval { $class->identify(\$tmp) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $class->identify(\$tmp) } } 36 18 54
186 18 ▶ return $feed->error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 18 18
187 }
188 0 return $feed->error("Cannot detect feed type");
189 }
190  
191 54 ▶sub _get_first_tag {
Subroutine _get_first_tag: called CC=5 SCAR=16.2
192 54 my $class = shift;
193 54 my ($xml) = @_;
194  
195  
196 ## Auto-detect feed type based on first element. This is prone
197 ## to breakage, but then again we don't want to parse the whole
198 ## feed ourselves.
199 54 my $tag;
200 54 ▶ while ($$xml =~ /<(\S+)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$xml =~ /<(\S+)/gs) 114 0 114
201 114 (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
202 114 my $first = substr $t, 0, 1;
203 114 ▶ $tag = $t, last unless $first eq '?' || $first eq '!';
Branch: 1 branch
BranchTrueFalseTotal
unless $first eq "?" or $first eq "!" 54 60 114
Condition100%3/3
$first eq "?" or $first eq "!"
l!l&&r!l&&!r
58254
MC/DC100%2/2
$first eq "?" or $first eq "!"
$first eq "?" $first eq "!"
Truth table100%3/3
A or B
A= $first eq "?"B= $first eq "!"
ABresult
1X1
011
000
204 }
205 54 ▶ die ("Cannot find first element") unless $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag 0 54 54
206 54 $tag =~ s/^.*://;
207 54 return $tag;
208 }
209  
210 0 ▶sub find_feeds {
Subroutine find_feeds: not called CC=2 SCAR=17.9
Pod documented
211 0 my $class = shift;
212 0 my($uri) = @_;
213 0 ▶ my @feeds = Feed::Find->find($uri)
Branch: 1 branch
BranchTrueFalseTotal
my(@feeds) = "Feed::Find"->find($uri) or return $class->error("Feed::Find"->errstr) 0 0 0
214 or return $class->error(Feed::Find->errstr);
215 0 @feeds;
216 }
217  
218 2 ▶sub convert {
Subroutine convert: called CC=4 SCAR=13.9
Pod documented
219 2 my $feed = shift;
220 2 my($format) = @_;
221 2 my $new = XML::Feed->new($format);
222 2 for my $field (qw( title link description language author copyright modified generator )) {
223 16 my $val = $feed->$field();
224 16 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 5 11 16
225 11 $new->$field($val);
226 }
227 2 for my $entry ($feed->entries) {
228 3 $new->add_entry($entry->convert($format));
229 }
230 2 $new;
231 }
232  
233 2 ▶sub splice {
Subroutine splice: called CC=3 SCAR=11.0
Pod documented
234 2 my $feed = shift;
235 2 my($other) = @_;
236 2 my %ids = map { $_->id => 1 } $feed->entries;
237 2 for my $entry ($other->entries) {
238 4 ▶ $feed->add_entry($entry) unless $ids{$entry->id}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids{$entry->id}++ 2 2 4
239 }
240 }
241  
242 8 ▶sub _convert_entry {
Subroutine _convert_entry: called CC=2 SCAR=7.1
243 8 my $feed = shift;
244 8 my $entry = shift;
245 8 my $feed_format = ref($feed); $feed_format =~ s!^XML::Feed::Format::!!;
246 8 my $entry_format = ref($entry); $entry_format =~ s!^XML::Feed::Entry::Format::!!;
247 8 ▶ return $entry if $entry_format eq $feed_format;
Branch: 1 branch
BranchTrueFalseTotal
if $entry_format eq $feed_format 8 0 8
248 0 return $entry->convert($feed_format);
249 }
250  
251 0 ▶sub useragent {
Subroutine useragent: not called CC=5 SCAR=34.0
Pod documented
252 0 my $feed = shift;
253 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
254 # Setter: validate the useragent
255 0 my $ua = shift;
256 0 ▶ unless (blessed($ua) && $ua->isa('LWP::UserAgent')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($ua) and $ua->isa("LWP::UserAgent")) 0 0 0
Condition0%0/3
builtin::blessed($ua) and $ua->isa("LWP::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($ua) and $ua->isa("LWP::UserAgent")
builtin::blessed($ua) $ua->isa("LWP::UserAgent")
Truth table0%0/3
A and B
A= builtin::blessed($ua)B= $ua->isa("LWP::UserAgent")
ABresult
0X0
100
111
257 0 Carp::croak("useragent must be an LWP::UserAgent object");
258 }
259 0 $feed->{useragent} = $ua;
260 0 return $ua;
261 } else {
262 # Getter: return existing or create new one
263 0 ▶ unless ($feed->{useragent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($feed->{"useragent"}) 0 0 0
264 0 $feed->{useragent} = LWP::UserAgent->new;
265 0 $feed->{useragent}->agent(__PACKAGE__ . "/$VERSION");
266 0 $feed->{useragent}->env_proxy;
267 }
268 0 return $feed->{useragent};
269 }
270 }
271  
272 sub base;
273 sub format;
274 sub title;
275 sub link;
276 sub self_link;
277 sub description;
278 sub language;
279 sub author;
280 sub copyright;
281 sub modified;
282 sub generator;
283 sub add_entry;
284 sub entries;
285 sub as_xml;
286 sub id;
287 sub image;
288  
289 4 ▶sub tagline { shift->description(@_) }
Subroutine tagline: called CC=1 SCAR=0.0
Pod documented
290 0 ▶sub items { $_[0]->entries }
Subroutine items: not called CC=1 SCAR=6.9
Pod documented
291  
292 # RFC 5005
293 sub first_link;
294 sub last_link;
295 sub previous_link;
296 sub next_link;
297 sub current_link;
298 sub prev_archive_link;
299 sub next_archive_link;
300  
301 35 1;
302 __END__
303  
304 =head1 NAME
305  
306 XML::Feed - Syndication feed parser and auto-discovery
307  
308 =head1 SYNOPSIS
309  
310 use XML::Feed;
311 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
312 or die XML::Feed->errstr;
313 print $feed->title, "\n";
314 for my $entry ($feed->entries) {
315 }
316  
317 ## Find all of the syndication feeds on a given page, using
318 ## auto-discovery.
319 my @feeds = XML::Feed->find_feeds('http://example.com/');
320  
321 =head1 DESCRIPTION
322  
323 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
324 also implements feed auto-discovery for finding feeds, given a URI.
325  
326 I<XML::Feed> supports the following syndication feed formats:
327  
328 =over 4
329  
330 =item * RSS 0.91
331  
332 =item * RSS 1.0
333  
334 =item * RSS 2.0
335  
336 =item * Atom
337  
338 =back
339  
340 The goal of I<XML::Feed> is to provide a unified API for parsing and using
341 the various syndication formats. The different flavors of RSS and Atom
342 handle data in different ways: date handling; summaries and content;
343 escaping and quoting; etc. This module attempts to remove those differences
344 by providing a wrapper around the formats and the classes implementing
345 those formats (L<XML::RSS> and L<XML::Atom::Feed>). For example, dates are
346 handled differently in each of the above formats. To provide a unified API for
347 date handling, I<XML::Feed> converts all date formats transparently into
348 L<DateTime> objects, which it then returns to the caller.
349  
350 =head1 USAGE
351  
352 =head2 XML::Feed->new($format)
353  
354 =head2 XML::Feed->new($format, \%args)
355  
356 Creates a new empty I<XML::Feed> object using the format I<$format>.
357  
358 $feed = XML::Feed->new('Atom');
359 $feed = XML::Feed->new('RSS');
360 $feed = XML::Feed->new('RSS', version => '0.91');
361  
362 An optional hashref of arguments can be provided as the last parameter:
363  
364 my $ua = LWP::UserAgent->new;
365 $ua->timeout(30);
366 $feed = XML::Feed->new('Atom', { useragent => $ua });
367  
368 Currently supported arguments:
369  
370 =over 4
371  
372 =item * useragent
373  
374 An L<LWP::UserAgent> object (or subclass) to use for fetching feeds via URI.
375 If not provided, a default UserAgent will be created when needed.
376  
377 =back
378  
379 =head2 XML::Feed->parse($stream)
380  
381 =head2 XML::Feed->parse($stream, \%args)
382  
383 =head2 XML::Feed->parse($stream, $format)
384  
385 =head2 XML::Feed->parse($stream, $format, \%args)
386  
387 Parses a syndication feed identified by I<$stream> and returns an
388 I<XML::Feed> object. I<$stream> can be any
389 one of the following:
390  
391 =over 4
392  
393 =item * Scalar reference
394  
395 A reference to string containing the XML body of the feed.
396  
397 =item * Filehandle
398  
399 An open filehandle from which the feed XML will be read.
400  
401 =item * File name
402  
403 The name of a file containing the feed XML.
404  
405 =item * URI object
406  
407 A URI from which the feed XML will be retrieved.
408  
409 =back
410  
411 I<$format> allows you to override format guessing.
412  
413 An optional hashref of arguments can be provided as the last parameter:
414  
415 my $ua = LWP::UserAgent->new;
416 $ua->timeout(30);
417 $feed = XML::Feed->parse('https://example.com/feed', { useragent => $ua });
418  
419 Currently supported arguments:
420  
421 =over 4
422  
423 =item * useragent
424  
425 An L<LWP::UserAgent> object (or subclass) to use for fetching feeds via URI.
426 If not provided, a default UserAgent will be created when needed.
427  
428 =back
429  
430 =head2 XML::Feed->get_file($filename)
431  
432 Gets a feed from a file.
433  
434 =head3 XML::Feed->get_fh($fh)
435  
436 Gets a feed from pre-opened filehandle.
437  
438 =head2 XML::Feed->get_uri($uri)
439  
440 Gets a feed from a URI.
441  
442 =head2 XML::Feed->find_feeds($uri)
443  
444 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
445 from that page (using I<E<lt>linkE<gt>> tags).
446  
447 Returns a list of feed URIs.
448  
449 =head2 XML::Feed->identify_format(\$xml)
450  
451 Given the xml of a feed return what format it is in, with C<Atom> or C<RSS> for
452 all versions of RSS. Note that you pass in a scalar ref to the xml string.
453  
454 =head2 $feed->convert($format)
455  
456 Converts the I<XML::Feed> object into the I<$format> format, and returns
457 the new object.
458  
459 =head2 $feed->splice($other_feed)
460  
461 Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
462 skipping posts that are already in I<$feed>.
463  
464 =head2 $feed->format
465  
466 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
467  
468 =head2 $feed->title([ $title ])
469  
470 The title of the feed/channel.
471  
472 =head2 $feed->base([ $base ])
473  
474 The url base of the feed/channel.
475  
476 =head2 $feed->link([ $uri ])
477  
478 The permalink of the feed/channel.
479  
480 =head2 $feed->tagline([ $tagline ])
481  
482 The description or tagline of the feed/channel.
483  
484 =head2 $feed->description([ $description ])
485  
486 Alias for I<$feed-E<gt>tagline>.
487  
488 =head2 $feed->author([ $author ])
489  
490 The author of the feed/channel.
491  
492 =head2 $feed->language([ $language ])
493  
494 The language of the feed.
495  
496 =head2 $feed->copyright([ $copyright ])
497  
498 The copyright notice of the feed.
499  
500 =head2 $feed->modified([ $modified ])
501  
502 A I<DateTime> object representing the last-modified date of the feed.
503  
504 If present, I<$modified> should be a I<DateTime> object.
505  
506 =head2 $feed->generator([ $generator ])
507  
508 The generator of the feed.
509  
510 =head2 $feed->self_link ([ $uri ])
511  
512 The Atom Self-link of the feed:
513  
514 L<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
515  
516 A string.
517  
518 =head2 $feed->entries
519  
520 A list of the entries/items in the feed. Returns an array containing
521 L<XML::Feed::Entry> objects.
522  
523 =head2 $feed->items
524  
525 A synonym (alias) for C<$feed-E<gt>entries>.
526  
527 =head2 $feed->add_entry($entry)
528  
529 Adds an entry to the feed. I<$entry> should be an L<XML::Feed::Entry>
530 object in the correct format for the feed.
531  
532 =head2 $feed->as_xml
533  
534 Returns an XML representation of the feed, in the format determined by
535 the current format of the I<$feed> object.
536  
537 =head2 $feed->useragent([ $ua ])
538  
539 Get or set the L<LWP::UserAgent> object used for fetching feeds.
540  
541 If called without arguments, returns the current UserAgent object,
542 creating a default one if it doesn't exist.
543  
544 If called with an L<LWP::UserAgent> object (or subclass), sets it as
545 the UserAgent for this feed.
546  
547 my $ua = LWP::UserAgent->new;
548 $ua->timeout(30);
549 $feed->useragent($ua);
550
551 # Later, retrieve it
552 my $current_ua = $feed->useragent;
553  
554 =head2 $feed->first_link ([ $uri ])
555  
556 The Atom First-link for feed paging and archiving (RFC 5005).
557  
558 L<http://tools.ietf.org/html/rfc5005>
559  
560 =head2 $feed->last_link ([ $uri ])
561  
562 The Atom Last-link for feed paging and archiving.
563  
564 =head2 $feed->next_link ([ $uri ])
565  
566 The Atom Next-link for feed paging and archiving.
567  
568 =head2 $feed->previous_link ([ $uri ])
569  
570 The Atom Previous-link for feed paging and archiving.
571  
572 =head2 $feed->current_link ([ $uri ])
573  
574 The Atom Current-link for feed paging and archiving.
575  
576 =head2 $feed->next_archive_link ([ $uri ])
577  
578 The Atom Next-link for feed paging and archiving.
579  
580 =head2 $feed->prev_archive_link ([ $uri ])
581  
582 The Atom Prev-Archive-link for feed paging and archiving.
583  
584 =head1 PACKAGE VARIABLES
585  
586 =over 4
587  
588 =item C<$XML::Feed::Format::RSS::PREFERRED_PARSER>
589  
590 If you want to use another RSS parser class than XML::RSS (default), you can
591 change the class by setting C<$PREFERRED_PARSER> variable in the
592 XML::Feed::Format::RSS package.
593  
594 $XML::Feed::Format::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
595  
596 B<Note:> this will only work for parsing feeds, not creating feeds.
597  
598 B<Note:> Only C<XML::RSS::LibXML> version 0.3004 is known to work at the moment.
599  
600 =item C<$XML::Feed::MULTIPLE_ENCLOSURES>
601  
602 Although the RSS specification states that there can be at most one enclosure per item
603 some feeds break this rule.
604  
605 If this variable is set then C<XML::Feed> captures all of them and makes them available as a list.
606  
607 Otherwise it returns the last enclosure parsed.
608  
609 B<Note:> C<XML::RSS> version 1.44 is needed for this to work.
610  
611 =back
612  
613 =cut
614  
615 =head1 VALID FEEDS
616  
617 For reference, this cgi script will create valid, albeit nonsensical feeds
618 (according to C<http://feedvalidator.org> anyway) for Atom 1.0 and RSS 0.90,
619 0.91, 1.0 and 2.0.
620  
621 #!perl -w
622  
623 use strict;
624 use CGI;
625 use CGI::Carp qw(fatalsToBrowser);
626 use DateTime;
627 use XML::Feed;
628  
629 my $cgi = CGI->new;
630 my @args = ( $cgi->param('format') // "Atom" );
631 push @args, ( version => $cgi->param('version') ) if $cgi->param('version');
632  
633 my $feed = XML::Feed->new(@args);
634 $feed->id("http://".time.rand()."/");
635 $feed->title('Test Feed');
636 $feed->link($cgi->url);
637 $feed->self_link($cgi->url( -query => 1, -full => 1, -rewrite => 1) );
638 $feed->modified(DateTime->now);
639  
640 my $entry = XML::Feed::Entry->new();
641 $entry->id("http://".time.rand()."/");
642 $entry->link("http://example.com");
643 $entry->title("Test entry");
644 $entry->summary("Test summary");
645 $entry->content("Foo");
646 $entry->modified(DateTime->now);
647 $entry->author('test@example.com (Testy McTesterson)');
648 $feed->add_entry($entry);
649  
650 my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml";
651 print $cgi->header($mime);
652 print $feed->as_xml;
653  
654  
655 =head1 LICENSE
656  
657 I<XML::Feed> is free software; you may redistribute it and/or modify it
658 under the same terms as Perl itself.
659  
660 =head1 AUTHOR & COPYRIGHT
661  
662 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008
663 Six Apart. All rights reserved.
664  
665 =head1 SUPPORT
666  
667 For support contact the XML::Feed mailing list - xml-feed@perlhacks.com.
668  
669 =head1 SOURCE CODE
670  
671 The latest version of I<XML::Feed> can be found at
672  
673 http://github.com/davorg/XML-Feed
674  
675 =cut
« Feed/Entry/Format/RSS.pm ↑ Summary Feed/Util.pm »