Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by haj (Vicar)
on Mar 08, 2023 at 19:04 UTC ( [id://11150860]=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

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2025-06-13 19:01 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.