Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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 scrutinizing the Monastery: (9)
As of 2014-08-20 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (109 votes), past polls