karlgoethebier has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
here is what did:
The role:
package Utils; use URI; use List::MoreUtils qw(uniq); use Types::Standard qw(InstanceOf); use Moo::Role; requires qw(list monks); sub my_uniq { my $self = shift; my @result = uniq @{ $self->list }; wantarray ? @result : scalar @result; } has 'uri' => ( is => 'lazy', isa => InstanceOf ['URI'], default => sub { my $self = shift; URI->new( $self->monks ) }, handles => [qw(host)], ); 1; __END__
The class:
package Karl; use Types::Standard qw(ArrayRef Str); use Moo; has list => ( is => 'lazy', isa => ArrayRef ); has monks => ( is => 'lazy', isa => Str ); with qw(Utils); 1; __END__
And the script:
#!/usr/bin/env perl use strict; use warnings; use feature qw(say); use Data::Dump; use Karl; my $monks = qq(http://www.perlmonks.org); my $list = [qw(cuke cuke)]; my $nose = Karl->new( list => $list, monks => $monks ); dd $nose; my $uniq = $nose->my_uniq; say $uniq; my @uniq = $nose->my_uniq; say for @uniq; say $nose->host; __END__
This works as expected and i hope that it is good practice. OK, i'm a bit unsure about this :-(
But i wonder how to handle the call to uniq like the call to host.
I would like to say $nose->uniq, instead of $nose->my_uniq.
In other words: how can i proxy (or delegate) a sub from a non OO module?
I must admit that i'm a bit confused for the moment.
Best regards and thank you very much for any hint, Karl
«The Crux of the Biscuit is the Apostrophe»
|
|---|