Contributed by PsychoSpunk on Jun 21, 2000 at 23:37 UTC
Q&A  > 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 = @_; ... }


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

Answer: How do I make deterministic constructors?
contributed by btrott

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?
Answer: How do I make deterministic constructors?
contributed by Perlmage

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.

Answer: How do I make deterministic constructors?
contributed by herveus



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.


Answer: How do I make deterministic constructors?
contributed by QandAEditors

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).

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.