rastoboy has asked for the wisdom of the Perl Monks concerning the following question:
Lately I've really been getting into OO Perl (and OO programming in general) and I'm kindof having a hard time deciding, sometimes, what should go into my "new" method. Should it be a barebones outline of the object? Should it contain useful data per se?
I decided to consult some random cpan modules on my machine. For example, Archive::New:
and that seems pretty reasonable, sets up kindof a basic structure with a few checks to see if an object is possible. Fair enough. But then I see a lot of like what HTML::Parser does:sub new { my $class = shift; my $self = bless( { 'diskNumber' => 0, 'diskNumberWithStartOfCentralDirectory' => 0, 'numberOfCentralDirectoriesOnThisDisk' => 0, # shld be # +of members 'numberOfCentralDirectories' => 0, # shld be # +of members 'centralDirectorySize' => 0, # must re-compute on write 'centralDirectoryOffsetWRTStartingDiskNumber' => 0, # must re-compute 'writeEOCDOffset' => 0, 'writeCentralDirectoryOffset' => 0, 'zipfileComment' => '', 'eocdOffset' => 0, 'fileName' => '' }, $class ); $self->{'members'} = []; my $fileName = ( ref( $_[0] ) eq 'HASH' ) ? shift->{filename} : sh +ift; if ($fileName) { my $status = $self->read($fileName); return $status == AZ_OK ? $self : undef; } return $self; }
so here we just get a very basic blessing of an empty hashref into the class, and then a sub "init" does some more heavy lifting of actually defining a unique sort of object. But I find myself writing "new" methods much like REST::Google here:sub new { my $class = shift; my $self = bless {}, $class; return $self->init(@_); } sub init { my $self = shift; $self->_alloc_pstate; my %arg = @_; my $api_version = delete $arg{api_version} || (@_ ? 3 : 2); if ($api_version >= 4) { require Carp; ...
I mean, they do all kinds of stuff in there, really kindof clearing the decks for action right off the bat.sub new { my $class = shift; my $args = $class->_get_args(@_); croak "attempting to perform request without setting a service + URL" unless ( defined $class->service ); my $uri = URI->new( $class->service ); $uri->query_form( $args ); unless ( defined $class->http_referer ) { carp "attempting to search without setting a valid htt +p referer header"; $class->http_referer( DEFAULT_REFERER ); } my $request = HTTP::Request->new( GET => $uri, [ 'Referer', $c +lass->http_referer ] ); my $ua = LWP::UserAgent->new(); $ua->env_proxy; my $response = $ua->request( $request ); croak sprintf qq/HTTP request failed: %s/, $response->status_l +ine unless $response->is_success; my $content = $response->content; my $json = JSON::Any->new(); my $self = $json->decode($content); return bless $self, $class; } sub new { my $class = shift; my $args = $class->_get_args(@_); croak "attempting to perform request without setting a service + URL" unless ( defined $class->service ); my $uri = URI->new( $class->service ); $uri->query_form( $args ); unless ( defined $class->http_referer ) { carp "attempting to search without setting a valid htt +p referer header"; $class->http_referer( DEFAULT_REFERER ); } my $request = HTTP::Request->new( GET => $uri, [ 'Referer', $c +lass->http_referer ] ); my $ua = LWP::UserAgent->new(); $ua->env_proxy; my $response = $ua->request( $request ); croak sprintf qq/HTTP request failed: %s/, $response->status_l +ine unless $response->is_success; my $content = $response->content; my $json = JSON::Any->new(); my $self = $json->decode($content); return bless $self, $class; }
So...which way is right? (if any of these are)?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Philosophy of a "new" method
by CountZero (Bishop) on Feb 04, 2011 at 07:14 UTC | |
|
Re: Philosophy of a "new" method
by JavaFan (Canon) on Feb 04, 2011 at 07:39 UTC | |
by rastoboy (Monk) on Feb 04, 2011 at 15:07 UTC | |
by BrowserUk (Patriarch) on Feb 04, 2011 at 16:11 UTC | |
by JavaFan (Canon) on Feb 04, 2011 at 15:35 UTC | |
|
Re: Philosophy of a "new" method
by wind (Priest) on Feb 04, 2011 at 07:36 UTC | |
|
Re: Philosophy of a "new" method
by tospo (Hermit) on Feb 04, 2011 at 10:06 UTC | |
by JavaFan (Canon) on Feb 04, 2011 at 17:23 UTC | |
by tospo (Hermit) on Feb 07, 2011 at 09:34 UTC | |
by JavaFan (Canon) on Feb 07, 2011 at 15:26 UTC | |
by tospo (Hermit) on Feb 09, 2011 at 10:37 UTC | |
|
Re: Philosophy of a "new" method
by pajout (Curate) on Feb 04, 2011 at 10:06 UTC | |
|
Re: Philosophy of a "new" method
by stonecolddevin (Parson) on Feb 04, 2011 at 22:18 UTC | |
by rastoboy (Monk) on Feb 05, 2011 at 01:28 UTC |