> 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


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.