Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do I make deterministic constructors?

by PsychoSpunk (Hermit)
on Jun 21, 2000 at 23:37 UTC ( [id://19315]=perlquestion: print w/replies, xml ) Need Help??

PsychoSpunk has asked for the wisdom of the Perl Monks concerning the following question: (object-oriented programming)

I'm wanting to provide a bit of determinism to the constructor method, and was wondering if I can simply make
sub new { my $value1, $value2 = @_; ... }

and

sub new { my $only_value = shift; ... (not the same as above ...) }
Am I looking for too much here? Or is there just no way to really effectively pull this one off other than putting it in one constructor that does the logic and passes off to other subs based on the values it gets? Thanx. Mike

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I make deterministic constructors?
by btrott (Parson) on Jun 21, 2000 at 23:49 UTC
    You mean can you make your constructors prototyped so that the correct constructor gets invoked based on the number of parameters you provide, like you might do in C++?

    I don't think you can do this in Perl, because Perl's version of prototypes isn't the same as C++'s. Differently-prototyped subroutines with the same name don't fly in Perl. I don't think.

    My question would be: are you sure you need to do this? Perhaps there's a better way of handling this in Perl that doesn't require such a thing.

    One way to do it would be to pass in the parameters as a hash, then just dump that hash into the hash-reference object that you create:

    package R; sub new { my $this = shift; my $class = ref($this) || $this; my $self = { @_ }; bless $self, $class; }
    Now people can create a new R object any way they please:
    my $r1 = new R; my $r2 = new R (foo => 'bar'); my $r3 = new R (baz => 'quux', foo => 'bar');
    and so on. Would that work for you?
Re: How do I make deterministic constructors?
by Perlmage (Acolyte) on Sep 18, 2000 at 19:24 UTC

    The short answer is: you can't. Perl doesn't do multiple multiple dispatch.

    The long answer is: Use the Class::Multimethods module. As is the case with many of the things that "Perl doesn't do", Damian Conway has made Perl do it.

Re: How do I make deterministic constructors?
by herveus (Prior) on Aug 29, 2001 at 18:10 UTC
    Howdy!

    Consider:

    sub new { if (@_ == 2) { new_1(@_); } elsif (@_ == 1) { new_2(@_); } else { warn "bad parameter..."; } }
    where subs new_1 and new_2 are your alternatives. You can do more detailed checking of the characteristics of the contents of @_ as needed. Of course, there is Class:Multimethods as mentioned earlier to automate this.

    yours,
    Michael

      I'd suggest using the goto &subroutine syntax for this case:

      sub new { if (@_ == 2) { goto &new_1; } elsif (@_ == 1) { goto &new_2; } else { warn "bad parameter..."; } }
      The benefit of this is that in every respect (e.g. for caller or croak) it looks like new_1 (or new_2) have been called directly - and not through new.

      -- Hofmator

Re: How do I make deterministic constructors?
by Anonymous Monk on Jun 28, 2000 at 20:07 UTC
    While waiting for an answer, I've been playing with a few ideas. I've touched on passing hashes into the constructor, as well as lists. I guess I was hoping that it could be done in what I view to be a simplistic way instead of either handling cases where I look for the existence of x to determine which route to apply, or by having multiple methods which are constructors (in the sense that they bless) that follow no particular naming convention. Thanks for the confirmation that I wasn't overlooking something (I'm just now dabbling in the black arts of Perl OO, and have not yet purchased the OO Perl book).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://19315]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found