Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found