in reply to Re^2: perl 5.38: can the body of a class declaration contain arbitrary code?
in thread perl 5.38: can the body of a class declaration contain arbitrary code?
There is a place for code which goes into the constructor: It is the ADJUST block. So you can have both: Class initialization as in my $next_count = 1, and constructor code.
"Class methods" in Perl 5.38 are just subs. So, to expand your example:
use 5.038; use feature 'class'; no warnings 'experimental'; class WithACounter { my $next_count = 1; # class initialization, called once my sub last_one_was_bad() { 0 } my sub delete_last_one() { ...; } field $count = $next_count++; ADJUST { if (last_one_was_bad()) { delete_last_one(); $next_count--; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: perl 5.38: can the body of a class declaration contain arbitrary code?
by jdporter (Paladin) on Jul 05, 2023 at 18:59 UTC | |
by haj (Vicar) on Jul 05, 2023 at 22:18 UTC | |
by jdporter (Paladin) on Jul 05, 2023 at 22:35 UTC | |
by cavac (Prior) on Jul 07, 2023 at 09:34 UTC |