Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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?

In reply to Re: How do I make deterministic constructors? by btrott
in thread How do I make deterministic constructors? by PsychoSpunk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found