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
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |