More and more I am building my Perl applications in a nice an clean OO way, with constructors, inheritance, prototypes, you name it. Lately I'm finding myself repeating the same few lines of code in every single instance method;
And the same with 'ref' negated for each class method. This has become my generic valid invocation checker... I know 'ref' should probably be 'blessed', but my Perl knowlegde may be a bit out of date. For class methods, $self should in fact probably be checked with /\W/, and possibly another check to see if the namespace is defined.sub foo { my $self = shift; croak "I am an instance method" unless $self and ref $self and $self->isa(__PACKAGE__); # actual body of code }
I was wondering if it might make sence to add 'modifiers' to the 'sub' keyword that would indicate to the interpreter to do checks like this... Something like:
method sub {} # $SELF=shift, check $SELF & isa instance method sub {} # =method modifier + ref $SELF class method sub {} # =method modifier + !ref $SELF & /\W/ & de +fined namespace
The 'method' modifier would setup $SELF automagically for instance. This would 'standardize' the syntax used in method code, while still allowing 'old-fashioned' code.
Maybe I've been working in Java too much, but this kind of run-time checking would greatly enhance my coding experience I think...
Naturally you guys will tell me that Perl 6 has all these things, and I will be shamed for not knowing enough about Perl 6... :)
In reply to OO-style modifiers for 'sub' ? by Gilimanjaro
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |