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

What has Moose got to do with it?

http://cpansearch.perl.org/src/SIMONW/XML-Feed-0.43/lib/XML/Feed/Content.pm, so to monkeypatch

sub XML::Feed::Content::body_alt { shift->_var('body_alt', @_) }
now use ->body_alt like ->body, as getter/setter

Replies are listed 'Best First'.
Re^2: Moose? Add a new attribute to a XML::Feed::Content object?
by uG (Scribe) on Mar 28, 2011 at 18:54 UTC
    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.

Re^2: Moose? Add a new attribute to a XML::Feed::Content object?
by uG (Scribe) on Mar 28, 2011 at 15:15 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.