Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How do I make deterministic constructors?

by herveus (Prior)
on Aug 29, 2001 at 14:10 UTC ( [id://108757]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to How do I make deterministic constructors?

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

Replies are listed 'Best First'.
Re: Answer: How do I make deterministic constructors?
by Hofmator (Curate) on Aug 29, 2001 at 14: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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://108757]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.