perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
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" )
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.$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' );
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.
|
|---|