Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Qualified package variable access ( private methods )

by LanX (Saint)
on Dec 19, 2020 at 15:55 UTC ( [id://11125450]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Qualified package variable access ( s/lexical/private/ )
in thread Qualified package variable access

> we certainly should not call them "private" because that has an established, widely understood meaning which is different from what these variables are. I refer to private members of classes in OO languages such as C++ and Java.

I'm neither an expert on C++ nor Java.

Could you please explain the semantic difference to this demo of private methods achieved with my ?

use strict; use warnings; use 5.10.0; # ======================================== # class definition # ======================================== { package Class; use Carp qw/cluck/; use Data::Dump qw/pp dd/; # ----- constructor sub new { my ($class,@init)=@_; return bless {@init}, $class; } # ----- private methods my $private = sub { my ($self) = @_; cluck "I'm a PRIVATE METHOD of OBJ= " . pp $self ; }; # ----- public methods sub public { my ($self) = @_; cluck "I'm a PUBLIC METHOD of OBJ= " . pp $self ; warn " ... calling private methods...\n"; $self->$private(); } } # ======================================== # demo # ======================================== package main; my $obj = Class->new( ID => 1 ); my $obj2 = Class->new( ID => 2 ); $obj->public; $obj2->public; $obj->private; # fails #$obj->$private; # doesn't compile

-*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Sat Dec 19 16:53:21 C:/Perl_524/bin\perl.exe -w d:/tmp/pm/private_method.pl I'm a PUBLIC METHOD of OBJ= bless({ ID => 1 }, "Class") at d:/tmp/pm/p +rivate_method.pl line 37. Class::public(Class=HASH(0x1cc1e0)) called at d:/tmp/pm/private_me +thod.pl line 56 ... calling private methods... I'm a PRIVATE METHOD of OBJ= bless({ ID => 1 }, "Class") at d:/tmp/pm/ +private_method.pl line 29. Class::__ANON__(Class=HASH(0x1cc1e0)) called at d:/tmp/pm/private_ +method.pl line 40 Class::public(Class=HASH(0x1cc1e0)) called at d:/tmp/pm/private_me +thod.pl line 56 I'm a PUBLIC METHOD of OBJ= bless({ ID => 2 }, "Class") at d:/tmp/pm/p +rivate_method.pl line 37. Class::public(Class=HASH(0x1cc0d8)) called at d:/tmp/pm/private_me +thod.pl line 57 ... calling private methods... I'm a PRIVATE METHOD of OBJ= bless({ ID => 2 }, "Class") at d:/tmp/pm/ +private_method.pl line 29. Class::__ANON__(Class=HASH(0x1cc0d8)) called at d:/tmp/pm/private_ +method.pl line 40 Class::public(Class=HASH(0x1cc0d8)) called at d:/tmp/pm/private_me +thod.pl line 57 Can't locate object method "private" via package "Class" at d:/tmp/pm/ +private_method.pl line 61. Compilation exited abnormally with code 255 at Sat Dec 19 16:53:22

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

Replies are listed 'Best First'.
Re^5: Qualified package variable access ( private methods )
by jdporter (Paladin) on Dec 21, 2020 at 19:00 UTC

    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.
      > 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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-24 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found