Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Thoughts on new 'class' OO in upcoming perl

by Jenda (Abbot)
on Apr 10, 2023 at 11:20 UTC ( [id://11151559]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Thoughts on new 'class' OO in upcoming perl
in thread Thoughts on new 'class' OO in upcoming perl

bless() is an implementation detail, new() is the agreed upon name of the method you are supposed to call.

Jenda
1984 was supposed to be a warning,
not a manual!

Replies are listed 'Best First'.
Re^5: Thoughts on new 'class' OO in upcoming perl
by tobyink (Canon) on Apr 10, 2023 at 15:14 UTC

    Only by convention. Conventions can be changed.

    To an extent, Moose has already changed that convention. The new method generated by Moose can be a little inflexible, so many people write a wrapper constructor around it — new_from_foo or new_with_bar or whatever — and document that this should be used rather than calling new directly.

      That gradual change is something I'm not completely happy with - only I fail to come up with a better solution.

      Moose did change the convention only a bit: It offers its own new with a key/value API, but also has BUILDARGS and other magic like coercions which allow to mimic practically any existing constructor API. Corinna will not provide the same helpers for backward compatibility. I guess I can live with that, because during the last decade or so I adopted the key/value style anyway.

      The "bless vs. new" thing has hit me a lot harder in situations where serialization of objects is needed. Even complex Moose objects can be built from e.g. JSON or YAML strings, and then blessed into the appropriate class. This is no longer possible with Object::Pad or Corinna: You can (re)create their objects only by calling new, undergoing the same API restrictions and validations as for fresh objects. If an object, during its lifetime, achieves a state which is not available by calling new, then... object persistence gets a lot harder.

      At $job - 1, we used Moose, but new_from_foo in a Class wouldn't have passed a code review. The correct way was to introduce a Class::Builder::FromFoo which took foo as the constructor parameter and provided a build method; thus following the Builder pattern.

      I'm not sure there's a similar pattern for with_bar, I've never needed it there.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11151559]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2025-06-13 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.