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); }
In reply to Re: OO - best way to have protected methods
by radiantmatrix
in thread OO - best way to have protected methods
by gargle
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |