Hello Brothers,

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:

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; }
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 {}, $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; ...
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 $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; }
I mean, they do all kinds of stuff in there, really kindof clearing the decks for action right off the bat.

So...which way is right? (if any of these are)?


In reply to Philosophy of a "new" method by rastoboy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.