Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Learning Moose, and have a few questions.

by walkingthecow (Friar)
on May 13, 2013 at 12:21 UTC ( #1033283=perlquestion: print w/ replies, xml ) Need Help??
walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

First, some code to give an example of the Qs I have:
package Example; use Moose; use Net::LDAP; # Options related to SSH has [ 'host', 'user'] is => 'rw', isa => 'Str', required => 1; has 'ldap', is => 'rw'; has 'error', is => 'rw', isa => 'Str'; # Create the SSH expect object sub get_user { my $self = shift; my $user = $self->user; my $host = $self->host; $self->ldap( Net::LDAP->new( $self->host ) ); my $mesg = $self->ldap->bind; $mesg = $self->ldap->search( filter => "(uid=$$self-user)" ); ... }
The code above is just there to clarify my questions...

  1. Is there a Moose way to define named subroutine parameters? Doing something like this:
    sub foo { my $arg = shift; if (exists $arg->{'fruit'} ) { .... } } foo( fruit => 'banana' );
    There's a new Moose way of creating constructors, and defining what paramaters (arguments, options) those accept. Is there a Moose way to define subroutine parameters? The old way still works, but I'm just wondering.


  2. Here's how I was creating an LDAP connection before:
    my $ldap = Net::LDAP->new( $host ); my $self->{ldap} = $ldap;
    This still works, but I am wondering if there's a Moose way to do this. I've defined the attributes for my class... how do I deal with attributes of the object (i.e., self) that are objects from a different module?

Comment on Learning Moose, and have a few questions.
Select or Download Code
Re: Learning Moose, and have a few questions.
by moritz (Cardinal) on May 13, 2013 at 12:34 UTC
Re: Learning Moose, and have a few questions.
by tobyink (Abbot) on May 13, 2013 at 12:36 UTC

    "There's a new Moose way of creating constructors, and defining what paramaters (arguments, options) those accept. Is there a Moose way to define subroutine parameters?"

    No, but see MooseX::Method::Signatures, MooseX::Declare, etc.

    "I've defined the attributes for my class... how do I deal with attributes of the object (i.e., self) that are objects from a different module?"

    Maybe you want to be using lazy builders, and allowing for dependency injection...

    package Example; use Moose; use Net::LDAP; # Options related to SSH has [qw( host user )] => ( is => "ro", isa => "Str", required => 1, ); has ldap_class => ( is => "ro", builder => "_build_ldap_class", ); has ldap => ( is => "ro", lazy => 1, builder => "_build_ldap", ); has error => ( is => "rw", isa => "Str", ); sub _build_ldap_class { return "Net::LDAP"; } sub _build_ldap { my $self = shift; return $self->ldap_class->new( $self->host ); } sub get_user { my $self = shift; my $user = $self->user; my $host = $self->host; my $mesg = $self->ldap->bind; $mesg = $self->ldap->search(filter => "(uid=$user)"); ... }

    Now if somebody using your class wants to change how it does LDAP searches, they can create a subclass of Net::LDAP and do:

    my $eg = Example->new(ldap_class => "Net::MyLDAP", ...);

    ... and your Example class will magically use their subclassed Net::LDAP object.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      That is so cool! Thank you! I'm reading the documentation for Moose, as well as the Modern Perl chapter on Objects, and I don't really see anything like what you've just shown me. Personally, I think your example should be part of the docs, and the example above as well. Thanks again :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1033283]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-12-23 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls