Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

For a constructor of an object based on another object, 'copy' and 'clone' sound like the same verb to me. So it is nice that this is "very clear" for you in your context. With the context I have, I can't imagine what your 'copy' does.

The only ambiguity with $obj->new( ... ) is when the "..." part is incomplete. In that case it isn't clear if, for the parts not mentioned, whether you get empty (or "default") versions of those or versions based on what $obj had. So, as I said, in theory, $obj->new( ... ) is ambiguous. But there are many cases where the meaning is pretty obvious (in context).

For example, you can sometimes significantly simplify a class by not allowing partially constructed objects (to the point that I've seen that as a "best practice" in some environments). So you can have new() where not specifying part of the object is fatal. So it is obvious that nothing in $obj is going to override everything that is explicitly specified in the arguments to $obj->new( ... ).

A pretty natural pattern (particularly in a language like Perl and particularly in a place like CPAN) is to have a class that supports a number of "programmer preferences" (whether exceptions are thrown, use of whitespace in output or construction of strings, strict standards compliance vs. allowing convenient extensions, etc.). So, each object might have very little actual "instance" data but the user of the class can set a number of "options" to fit their particular environment or just personal preferences. So it makes sense to allow the user to create a factory object from which to create their objects. The factory would be configured with their particular preferences so that they don't need to specify those each time they construct a new object.

But once you get to that point, you likely also realize that the simplest way to achieve that is to just let the class be its own factory class. Then $obj->new( ... ) is the obvious syntax for using the preferences from $obj when constructing a new object. The only ambiguity is if $obj was given more than just preferences while ->new(...) was given nothing but preferences. But that's also in theory. In practice, details of the specific class might make it quite clear what happens in that case (or make that case impossible).

To be clear, I'm not arguing that it is inappropriate for a collection of somewhat related classes (such as those produced by a single team) to decide to studiously avoid $obj->new( ... ). Avoiding that can be a reasonable "best practice", especially in some environments.

If your framework prevents you from doing something so dead simple as croak()ing if the item passed to new() is a reference, then I think you should seriously evaluate your framework. There are places where generating methods can be a reasonable choice. But using a generated method should not be required. If your getter or setter has work to do other than just copying one scalar value uninspected, then I suggest you don't generate that particular getter or setter (just write it out).

If your framework does not even give you the option of writing your own new(), then that seems like a serious limitation (I don't know if Moo[se] precludes you from doing so or just causes you to lose too many "benefits" were you to do that). Of course, having a constructor that exposes all of your attribute names in its interface is also a horrible idea from a design perspective and is quite anti-modular, so I don't want the constructor Moose generates anyway. :)

Yeah, unconditional method generation sucks and can prevent you from doing trivial things or require you to do problematically complex work to accomplish what should be trivial.

- tye        


In reply to Re^3: $class = ref($class) if ref($class) Redux (frameworks) by tye
in thread $class = ref($class) if ref($class) Redux by boftx

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 chanting in the Monastery: (6)
As of 2024-03-28 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found