I am wondering if there exists a faster way?

What do you mean faster? Faster to write? Faster to run? Faster to read and understand? What are you trying to optimize for, or is this just a thought exercise?

I'd generally suggest using any of a number of class generating tools -- these usually have syntax which are fairly self documenting. Personally, I don't find that protected functions are really common in Perl unless you're an OO purist.

However, protected fields are already supported by Object::InsideOut, and in five statements of code:

package My::Class; use Object::InsideOut; my @FLYBEHAVIOUR :Field( 'Accessor' => 'FLYBEHAVIOR', 'Restricted' => +1 ); my @QUACKBEHAVIOUR :Field( 'Accessor' => 'QUACKBEHAVIOR', 'Restricted' + => 1 ); 1; # modules need to return true

Is that fast enough for you?

Implementing protected fields is somewhere on the TODO list for Class::InsideOut. However, if all you want is public/private, the syntax is similarly short and very readable:

package My::Class; use Class::InsideOut ':std'; private FLYBEHAVIOR => my %FLYBEHAVIOR; private QUACKBEHAVIOR => my %QUACKBEHAVIOR; sub new { register( bless \(my $scalar), shift ) } 1; # modules need to return true

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re: A quicker way to have protected and private fields? by xdg
in thread A quicker way to have protected and private fields? 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.