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

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

by haj (Vicar)
on Mar 07, 2023 at 18:50 UTC ( [id://11150833]=note: print w/replies, xml ) Need Help??


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

I understand that the warnings of being experimental and very incomplete are a bit scary. I'll write a separate article about what's missing in core OO, maybe with a frequently edited head article which will get smaller and smaller over time.

Yet, your article rises some interesting questions.

  • ...it would be interesting to benchmark bless-based and class-based new() functions.: I've tried to do this with one of my pet classes (which is not on CPAN because it is almost identical to Math::Vector::Real) in an application which creates hundreds of thousands of objects and couldn't find any significant differences. And I don't think this is too surprising. By the way: For this particular class, I consider a blessed array reference (i.e. no Moo* stuff) still to be an excellent alternative, but the current development state of core OO doesn't allow a comparison for complex classes yet.
  • One of the things that I was interested in testing was whether simply changing 'package' to 'class' would still work in the current framework...: There's quite a lot which just works after that change.
    • @INC variable: No problems here. The resolution mechanism between classes (which share their namespace with packages) to files is the same.
    • parent pragma: No. The hierarchy is defined in the class declaration like :isa(Parent::Class). Trying use parent ... fails with an error message Modification of a read-only value attempted at /home/haj/localperl/lib/5.37.10/parent.pm line 22. . The message might deserve some tuning, but the "read-only value" is actually the @ISA of your new class. Core OO populates that array, but makes it readonly for the rest of the world.
    • 'SUPER' pseudoclass: Works as usual.
  • The current documentation for class is extremely sparse. True! This is a known issue. Eventually, this chapter of perlfunc should be self-contained, with a link to perlclass. I admit that I tried to prepare a PR for that but got stuck between things which are still technically possible but ought to be forbidden (like having both a package and a class with the same name).
  • Comment on Re^2: Thoughts on new 'class' OO in upcoming perl

Replies are listed 'Best First'.
Re^3: Thoughts on new 'class' OO in upcoming perl
by kcott (Archbishop) on Mar 08, 2023 at 00:17 UTC

    G'day haj,

    ++ Thanks for looking into the points I raised and providing feedback.

    There wasn't anything that was actually "scary", at least for me. I usually try out experimental features privately when they first appear; I won't use them in production (personal, $work, or even PM code) until the experimental status is lifted. I wasn't too surprised with "very incomplete": I took class to be the first step in a staged implementation, wholly or partly, of Corinna.

    What you wrote about parent presumably also applies to base and any code that manipulates @ISA (either directly or via a module).

    That's an interesting point about package and class names. I had wondered about that but didn't list it with things I was interested in testing. Forbidding duplicate names seems appropriate; although, presumably something like this would be valid (even if it is poor design):

    class X; ... package Y; ... class X; ... extend the class here ...

    — Ken

      What you wrote about parent presumably also applies to base and any code that manipulates @ISA (either directly or via a module).

      Correct! In core Perl OO, @ISA is set to readonly as soon as the class declaration has been processed. I think it makes sense to maintain the value for reading.

      About your code example: That construct is currently a syntax error. Once a class has been "closed" by ending the enclosing block, file, or like in your example by opening another namespace with package Y;, that namespace is "sealed":

      Cannot reopen existing class "X"

      Everything else would be at least messy (consider e.g. conflicting parent classes or version declarations). And while this is a restriction compared to the sloppy package rules, I can not imagine a problem which can be adequately solved by re-opening a class.

      There is a bit of a grey zone. Code like this is "valid":

      use 5.37.9; use feature 'class'; no warnings 'experimental'; class X; field $f :param; method normal { say '$f = ', $f; } #---------------------------------------- package X; sub creepy { my $self = shift; say @_; } package main; my $x = X->new(f => "Hello, world"); $x->normal; $x->creepy("Goodbye, friends");

      So, as of today, you can re-open an existing namespace as a package, as always before. Subroutines can be written like old-style "methods" in that package. They are resolved as expected You can even override a method. The method gets the core OO object as its first parameter. But in those creepy methods you do not have access to the fields of the class with the same (nor can you write them as method, to begin with). Having code implanted into the namespace does not give special access to the object.

      Perl 5 is not known for being eager to prohibit creepy code, so I guess it will stay that way.

Re^3: Thoughts on new 'class' OO in upcoming perl
by LanX (Saint) on Mar 07, 2023 at 23:57 UTC
    > (like having both a package and a class with the same name).

    Please excuse my ignorance, if it's not for speed, why else?

    What is the incentive for those parallel structures instead of building on top resp. wrapping around existing ones?

    Like a class is a special package, a new is blessing a hash, etc ...?

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2025-06-13 20:05 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.