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)?
In reply to Philosophy of a "new" method by rastoboy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |