Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Answer: How do I make deterministic constructors?

( #108757=categorized answer: print w/ replies, xml ) Need Help??

Q&A > object-oriented programming > How do I make deterministic constructors? contributed by herveus

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

Comment on Answer: How do I make deterministic constructors?
Download Code
Re: Answer: How do I make deterministic constructors?
by Hofmator (Curate) on Aug 29, 2001 at 18:23 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-20 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls