Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by uG (Scribe)
on Mar 28, 2011 at 07:36 UTC ( #895855=perlquestion: print w/ replies, xml ) Need Help??
uG has asked for the wisdom of the Perl Monks concerning the following question:

sub get_by_id { my ($self, $feed_id) = @_; my $feed = $self->single({id => $feed_id}); 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 $_->content($translated_string); } return $xml_feed; }
The code above is meant to translate an rss feed.
[% FOREACH entry IN feed.entries %] TRANSLATED:[% entry.content.body %] [% END %]
The template above displays the translated content. However, if I would like to do
[% FOREACH entry IN feed.entries %] ORIGINAL:[% entry.content.body %] TRANSLATED:[% entry.content.body_alt %] [% END %]
It would seem I want to add an attribute (body_alt) to an XML::Feed::Content object. Am I on the right track? I want to believe $_->content->{body_alt} = $translated_string would do what I want, but i'm sure all you guys are about to point out why it doesn't do what I think it should (see second template).

Comment on Moose? Add a new attribute to a XML::Feed::Content object?
Select or Download Code
Re: Moose? Add a new attribute to a XML::Feed::Content object?
by Anonymous Monk on Mar 28, 2011 at 08:22 UTC
      By monkey patch, do you mean modifying my local version of XML::Feed::Content.pm? The reason I mention Moose is because I thought the right solution would be a Moose class that inherits everything from XML::Feed::Content, and then add the additional trait body_alt.

        Monkey patch means extend/modify code while running / at run time. So the suggestion means, just add that code to your actual script/program and not your locally installed module. Looks like a good suggestion for the short term. Might submit a patch/ticket to the module to get the feature for real down the road.

      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?

        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: perlquestion [id://895855]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (19)
As of 2014-12-22 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (118 votes), past polls