[pm-h] Moose question

Todd Rinaldo toddr at null.net
Mon Dec 21 08:25:10 PST 2009


I'm a big fan of lazy. You're kinda randomly initializing your object
in the object if you don't use it. Oh and per the Moose gods, don't
forget to turn off Moose and make immutable at the end of your module.

Try this:


#!/usr/bin/perl
use strict;
use Website;
my $web = Website->new(name ='Google', url ='http://www.google.com/');
print "My Website's name is " . $web->name . "\n";
print $web->get_content();

package Website;
use Moose;
use WWW::Mechanize;

# Mech will initialize whenever it's first called.
has '_mech'    => (isa => 'Object', is => 'ro', lazy => 1, default =>
sub { WWW::Mechanize->new });

has 'name'  =( is ='rw', isa ='Str', required =1);
has 'url'   =(is ='rw', isa ='Str', required =1);

sub get_content {
    my $self = shift;
    $self->_mech->get($self->url);
    return $self->_mech->content;
}

no Moose;
__PACKAGE__->meta->make_immutable;
1;


More information about the Houston mailing list