Re: Callback methods in a Moose object

[email protected] ("Toby Betts")
Newsgroups perl.moose
Message-ID <[email protected]>
Jesse Luehrs wrote:
> On Mon, Jan 09, 2012 at 05:42:39PM -0800, Toby Betts wrote:
> > The issue seems to be that the arguments to be given to the callback are
> > missing. Based on the XML::Twig docs, handlers are passed a self-referencing
> > object called a twig and the element to handle, like so:
> 
> Yes, this is the issue. If you're wrapping a method call in a coderef
> like that, you need to pass through the arguments yourself - the form
> "sub { $self->method }" is only correct when the callback doesn't take
> arguments. What you really want is "sub { $self->method(@_) }".

Thanks. With this input, I've found that including a CodeRef wrapper isn't
necessary. This code appears to work as desired:

  my $parser = XML::Twig->new(
    twig_roots => {
      'title'       => sub { $self->title_handler(@_) },
      'description' => sub { $self->description_handler(@_) },
      'item'        => sub { $self->item_handler(@_) },
    },
  );


Toby
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.