Just sort of "thinking out loud", here...

package Private; use strict; use warnings; use Carp; use vars qw'@ISA @EXPORT $VERSION $PACKAGE'; #===================================================================== +========= # Private.pm - a module to implement extremely simple private subs # this is a 'stub', which will hopefully be expanded upon + by # others. #--------------------------------------------------------------------- +--------- # use Private; # sub _my_private_sub { # is_private; # makes this sub package-private # } #--------------------------------------------------------------------- +--------- # (c)2005 Darren Meyer <darren.meyer@gmail.com> (see LICENSE at end of + file) #===================================================================== +========= $VERSION = '0.81'; require Exporter; @ISA = 'Exporter'; @EXPORT = 'is_private'; ## automatically set up the invoking package name on 'use' my $PACKAGE = caller(0); sub is_private() { my @call = caller(1); ## ok if caller of sub that called us was in the invoking package return 1 if $call[0] eq $PACKAGE; ## otherwise, create fatal error ## Package_Name cannot call sub_name (private to Other_Package) confess ( $call[0].' cannot call '.$call[3].' (private to '.$PACKAGE.')' +."\n" ); } 1; __END__ =head1 LICENSE This module is licensed under the MIT License, as stated here: Copyright (c)2005 Darren Meyer Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the ``Software''), t +o deal in the Software without restriction, including without limitation the rig +hts to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is furnish +ed to do so, subject to the following conditions: The above copyright notice and this permission notice shall be include +d in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPR +ESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILIT +Y, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHAL +L THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISIN +G FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. =cut

With the above in place, you could create your private add sub like:

use Private; sub _add { is_private; ## die if called outside of this package my $self = shift; my $amount = shift; $self->balance($self->balance+$amount); }
<-radiant.matrix->
Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

In reply to Re: OO - best way to have protected methods by radiantmatrix
in thread OO - best way to have protected methods by gargle

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.