Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Moose? Add a new attribute to a XML::Feed::Content object?

by uG (Scribe)
on Mar 28, 2011 at 18:54 UTC ( #895997=note: print w/ replies, xml ) Need Help??


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?


Comment on Re^2: Moose? Add a new attribute to a XML::Feed::Content object?
Select or Download Code
Re^3: Moose? Add a new attribute to a XML::Feed::Content object?
by Corion (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://895997]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2015-07-07 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls