One example might be you have a Backend class where your app sends all its data. You might instantiate the backend object like this:
my $backend = Backend->new(); Role::Tiny->apply_roles_to_object( $backend, qw( Backend::Storage::PostgreSQL Backend::Log::Debug Backend::Interf +ace::TCP ), );
And now your backend object will store its data in PostgreSQL, and spit out debugging information to its log file, and listen on a TCP port for instructions.
If you have eight different possibilities for storage (different types of database, XML files, JSON files, etc), 4 different options for logging, and 4 different interfaces (TCP, Unix Sockets, Command Line, and HTTP), that gives 128 possible combinations. You don't really want to have to create classes for Backend::PostgreSQL_Debugging_TCP, Backend::MySQL_Debugging_TCP, Backend::PostgreSQL_Debugging_HTTP, etc. Instead you just compose the combination of roles you want.
(In this particular example, there's an argument for making more use of delegation instead of composition, but I digress.)
In reply to Re: Role::Tiny: When to use apply_roles_to_object?
by tobyink
in thread Role::Tiny: When to use apply_roles_to_object?
by karlgoethebier
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |