Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How do you create attribute handlers for methods of an object (via Moose) ?

by nodebunny (Novice)
on Sep 26, 2011 at 18:55 UTC ( #927927=perlquestion: print w/replies, xml ) Need Help??
nodebunny has asked for the wisdom of the Perl Monks concerning the following question:

I think I've got attribute handlers down for perl Natives!
package tree; has '_branches' => ( traits => ['Hash'], is => 'rw', isa => 'HashRef[Any]', handles => { _set_branch => 'set', _is_branch => 'defined', _list_branches => 'keys', _branch => 'get' }, trigger => sub { my($self,$hash) = @_; $self->_build_branch($hash); } ); sub _build_branch{ my($self,$hash); # do stuff! #return altered or coerced hash return $hash; }
What do you think? But let's say for example I have a LinkedList object with the following methods
LinkedList{} LinkedList.append() LinkedList.insert() LinkedList.size() LinkedList.has_children() LinkedList.remove() LinkedList.split()
Is there a way handle the object's methods via Moose Attributes (without using MooseX) - similar to this?
package Bucket; has '_linkedlist' => ( traits => ['LinkedList'], is => 'rw', isa => 'LinkedListRef[Any]', handles => { _add_link => 'append', _insert_link => 'insert', _count_links => 'size', _del_link => 'remove', _split_at_link => 'split', _has_sublinks => 'has_children', } };
It would be great if there was a way to do this, but I'm concerned maybe I've misunderstood something somewhere about how or why to create handlers for non-native attributes. Your wisdom is appreciated!

Replies are listed 'Best First'.
Re: How do you create attribute handlers for methods of an object (via Moose) ?
by ikegami (Pope) on Sep 26, 2011 at 19:17 UTC

    This question was also posed on StackOverflow, where I answered the following.

    Are you simply overcomplicating things, or am I missing something?

    package Bucket; has '_linkedlist' => ( is => 'rw', isa => 'LinkedList', handles => { _add_link => 'append', _insert_link => 'insert', _count_links => 'size', _del_link => 'remove', _split_at_link => 'split', _has_sublinks => 'has_children', }, );

    Hashes don't have methods, which is why a trait is involves. The trait adds the methods. Your LinkedList class has methods, so no need to write a trait to provide the methods.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927927]
Approved by ikegami
[Corion]: Veltro: This is due to the fact that one of our webservers doesn't have "our" https certificate but another https certificate. Work is underway (well, on my long, long to-do list) to switch even that server over to "our" certificate
[Veltro]: Corion OK, thanks. At least I understand it now.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2018-05-23 11:29 GMT
Find Nodes?
    Voting Booth?