I've been reading through Conway's on object oriented programming and don't see any ready solutions. There are plenty of ways to enforce privacy of data, but he doesn't describe ways to *ensure* (for my own benefit) that other classes don't use private methods of other classes.

The only thing I've thought of is to keep all the private routines defined in a "setup" routine that stores all the pointers to the methods and future calls only go through indirect calls.

My solution to data privacy wasn't one suggested by Conway (perhaps with good reason?) but I decided to create a package "frame" - one/package. I can likely extend that to encompass my private methods.

My current package namespace looks something like this: (majorly trimmed output from DataDumper of the top-most class's ref returned to "main" )

$s = bless( { 'Entry_Display_in_Wins' => { _GUI_Glue' => bless( { 'GUI_Glue' => { '_image_win'=>bless( {},'Tk::Photo'), '_main_win' =>bless( {}, 'MainWindow'), } }, 'GUI_Glue' ) }, 'Entry_Ordering' => { '_auto_advance_time' => 6, '_chances' => [], '_chances_total' => 0, '_cur_entry_index' => -2, '_disable_auto_advance' => 0, '_formula_text' => '$x*$x', '_rating_to_chance' => sub () { package Entry_Ordering; use strict 'refs'; my $x = shift @_; $x = $x*$x;; }, '_record_index' => -1, '_shuffle' => 0, 'record' => [] }, 'File_Rating_DB' => { '_base_dirname' => '/home/$USER/img-redirect/', '_db_filename' => 'show.dat', '_db_modified' => 1, }, 'File_Rating_Entries' => { '_entries' => [ bless( { '_filename' => 'Andromeda-m31.jpg', '_rating' = +> 2 }, 'File_Entry' ), bless( { '_filename' => 'Hibiscus_1.jpg', '_rating' => 9 + }, 'File_Entry' ) ] } }, 'Timed_Display_In_Wins' );
So I was thinking -- I could segregate private methods by storing coderefs in "variables" and call indirectly through the variable names. I can do an access check to catch calls to "private methods" called from a different class.

B::Lint catches calls to private methods, but isn't too helpful since it flags *every* call to a "private method" (ones beginning with "_") and claims it may be broken on "most" (any thread-enabled) Perls.

Anyway, just hoping to get some feedback on how others enforce object "privacy". I haven't run into anything on perl-object privacy where objects include methods as well as data. Have seen a few on data_privacy, closures and the like, but the data is only "half" the object or class (and usually the bulk of the work! :-)).

I don't need method privacy at any greater level than that which I have above for class fields. I.e. the data is accessible if they explicitly go through class fields but no accidental accesses like I've had with methods. It can be especially problematic if you split an existing class (or set of linear actions into one or more hierarchical classes like package "Array_In_File" being split into "Array_Ops" and "File_Ops", and want to make sure no there's no "unauthorized" semi-private method hanky-panky. Linda W.


In reply to Howto keep private methods private? by perl-diddler

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.