in reply to Re^4: Qualified package variable access ( private methods )
in thread Qualified package variable access

Well, one major difference is that that $private is not a member of the class at all, it is a lexical in the same block in which you've defined the subs of the class. And, of course, it's singleton. A class private in C++/Java is not singleton. It could be declared 'static', which would make it singleton; but that still wouldn't be the same as what you've shown. If your $private were truly equivalent, you'd be able to write this:

use strict; use warnings; { package Class; my $n = 42; sub new { my $pkg = shift; bless {}, $pkg; } sub foo; } sub Class::foo { print "$n\n"; }

But you can't, of course, because $n is out of scope at that point. It is not a member of the "class".

I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Replies are listed 'Best First'.
Re^6: Qualified package variable access ( private methods )
by LanX (Saint) on Dec 21, 2020 at 20:36 UTC
    > But you can't, of course, because $n is out of scope at that point. It is not a member of the "class".

    I was aware of the scoping/closure effect, but I very rarely see Perl code where class definitions are dispersed over more the one file.

    I.O.W file scope is sufficient.

    And I think we aggree that Perl allows so many kinds of introspection (e.g. PadWalker) that privacy becomes relative.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery