6.6 Subroutines

     

You can define ordinary subroutines within a class or role. They cannot be invoked on an object with the $object.methodname syntax and will never pass an implicit invocant argument. They aren't inherited but can be pulled in with role composition. They're mainly useful for utility code internal to the class:

 class Answer::Young; has $.answer;  . . .  sub is_valid ($value) {     return 1 if 10 < $value < 42; }  . . .  method set_answer ($new) {     $.answer = $new if is_valid($new); } 

Subroutines may be exported if the class also functions as a module.



Perl 6 and Parrot Essentials
Perl 6 and Parrot Essentials, Second Edition
ISBN: 059600737X
EAN: 2147483647
Year: 2003
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net