package Attribute::Protected; use 5.006; use strict; use warnings; our $VERSION = '0.01'; use Attribute::Handlers; sub UNIVERSAL::Protected : ATTR(CODE) { my($package, $symbol, $referent, $attr, $data, $phase) = @_; my $meth = *{$symbol}{NAME}; no warnings 'redefine'; *{$symbol} = sub { unless (caller->isa($package)) { require Carp; Carp::croak "$meth() is a preotected method of $package!"; } goto &$referent; }; } sub UNIVERSAL::Private : ATTR(CODE) { my($package, $symbol, $referent, $attr, $data, $phase) = @_; my $meth = *{$symbol}{NAME}; no warnings 'redefine'; *{$symbol} = sub { unless (caller eq $package) { require Carp; Carp::croak "$meth() is a private method of $package!"; } goto &$referent; }; } sub UNIVERSAL::Public : ATTR(CODE) { my($package, $symbol, $referent, $attr, $data, $phase) = @_; # just a mark, do nothing } 1; __END__ =head1 NAME Attribute::Protected - implementing proctected methods with attributes =head1 SYNOPSIS package SomeClass; use Attribute::Protected; sub foo :Public { } sub _bar :Private { } sub _baz :Protected { } sub another { my $self = shift; $self->foo; # OK $self->_bar; # OK $self->_baz; # OK } package DerivedClass; @DerivedClass::ISA = qw(SomeClass); sub yetanother { my $self = shift; $self->foo; # OK $self->_bar; # NG: private method $self->_baz; # OK } package main; my $some = SomeClass->new; $some->foo; # OK $some->_bar; # NG: private method $some->_baz; # NG: protected method =head1 DESCRIPTION Attribute::Protected implements something like public / private / protected methods in C++ or Java. =head1 ATTRIBUTES =over 4 =item Public sub foo :Public { } just a mark. Can be called from everywhere. =item Private sub _bar :Private { } Can't be called from outside the class where it was declared. =item Protected sub _baz :Protected { } Can be called from the class where it was declared or its derived clas +ses. =back When called from inappropriate classes, those methods throw an exception like C<foo() is a protected method of Foo!>. =head1 AUTHOR Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt> =head1 SEE ALSO L<Attribute::Handlers>, L<Protect>, L<Class::Fields> =cut

In reply to Attribute::Protected by miyagawa

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.