Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Moose and OOP in Perl

by fritz1968 (Sexton)
on May 09, 2013 at 19:42 UTC ( #1032828=perlquestion: print w/replies, xml ) Need Help??
fritz1968 has asked for the wisdom of the Perl Monks concerning the following question:


I am lazy. That is why I code in Perl. And if a loop of some sort can set/define most of my variables, then I will use that and get the 'one-offs' later. Which brings me to my problem. I have the following code:
package LDAPclient; use Moose; my @ldap_attrs = qw( ssl host port retry timeout version loginID loginPW logging ); for my $name (@ldap_attrs) { my $reader = 'get_' . $name; my $writer = 'set_' . $name; has $name => ( is => 'rw', reader => $reader, writer => $writer, required => 0 ); }

I want to set default values for ssl, port, retry, timeout, version and logging... but I don't want to have 5 different "has" lines with each having the is, reader, writer, required and default parts. Is there a way to set a default for a value outside of the above loop? If so, I would appreciate an example.

thanks, frank

Replies are listed 'Best First'.
Re: Moose and OOP in Perl
by moritz (Cardinal) on May 09, 2013 at 20:00 UTC
    my %defaults = ( ssl => 1, port => 12345, timeout => 42, ); for my $name (@ldap_attrs) { my $reader = 'get_' . $name; my $writer = 'set_' . $name; my @default; if (exists $defaults{$name}) { @default = (default => $defaults{$name}); } has $name => ( is => 'rw', reader => $reader, writer => $writer, required => 0, @default, ); }

    Untested, but something along those lines should work.

Re: Moose and OOP in Perl
by tobyink (Abbot) on May 09, 2013 at 21:35 UTC

    moritz' answer is perfectly good. Here's another in the spirit of TIMTOWTDI. It uses builders instead of defaults. (Slightly slower object construction, but generally considered a cleaner way of doing things in terms of making it easier for subclassing.)

    my @ldap_attrs = qw( ssl host port retry timeout version loginID loginPW logging ); use constant { _build_ssl => 1, _build_port => 12345, _build_timeout => 42, }; has $_ => ( is => "rw", reader => "get_$_", writer => "set_$_", required => 0, __PACKAGE__->can("_build_$_") ? (builder => "_build_$_") : (), ) for @ldap_attrs;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032828]
Front-paged by Corion
[marto]: yes, the last time I saw them was in Berlin a few years ago, at the old Olympic stadium
[marto]: After Sunday I don't think I'll go see them again :)
[LanX]: Corion: come on, people are people! ;)
[Corion]: Naah, I think it's still an OK show so far. Their new songs aren't exactly great, but I'm not going there for new material anyway ;)
[marto]: I got the feeling from the last show that for big sections of it, they were not really into what they were doing
[Corion]: LanX: Sure, they can bask in my Halo
[marto]: more so than the previous show I saw
[Corion]: marto: Well, I think they go a tour every two years and I think it's hard to even get a connection with the crowd at a 20k people concert... But maybe after this time I'll stop too ;)
[Corion]: I still have to see the Pet Shop Boys live before they stop touring at all
[marto]: yeah, I think that as a group creatively they're done. I can understand how it'd be hard to stop the process, album/tour, album/tour, if that's pretty much all you've ever done :)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2017-03-24 11:39 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.