http://www.perlmonks.org?node_id=895997


in reply to Re: Moose? Add a new attribute to a XML::Feed::Content object?
in thread Moose? Add a new attribute to a XML::Feed::Content object?

sub get_by_id { my ($self, $feed_id) = @_; my $feed = $self->single({id => $feed_id}); sub XML::Feed::Content::body_alt { shift->_var('body_alt', @_) }; my $xml_feed = XML::Feed->parse(URI->new($feed->url)); $xml_feed->{'url'} = $feed->url; use WWW::Translate::Apertium; foreach ($xml_feed->entries) { my $engine = WWW::Translate::Apertium->new(lang_pair => 'en-ca'); my $translated_string = $engine->translate($_->content->body); #This change $_->content->body_alt $_->content->body_alt($translated_string); } return $xml_feed; }
I added in the monkey patch. However I am not getting the results I am expecting when I do
[% FOREACH entry IN feed.entries %] [% USE Dumper %] [% Dumper.dump(entry.content) %] [% END %]
Which shows us
$VAR1 = bless( { 'body' => 'This is a test post', 'base' => undef, 'ty +pe' => 'text/html' }, 'XML::Feed::Content' );
I see there is no body_alt. Now what am I doing wrong?

Replies are listed 'Best First'.
Re^3: Moose? Add a new attribute to a XML::Feed::Content object?
by Corion (Patriarch) on Mar 28, 2011 at 19:27 UTC

    It seems that ->entries does not return a premade list of entries for you to modify. It always returns a fresh list of entries, so your modifications are gone on your second call.

    I suggest that you ditch your idea of modifying XML::Feed and adding something to it, and switch to plain hashes, or alternatively add the method in question to XML::Feed::Content and call it from your template instead.