... delay the compilation to the last possible moment, and then keep the result for future invocations to use.
If one were dealing with a lexical variable as in your example, and if the class was defined entirely within a file, and if the file/module was loaded only via a use statement, what would be the advantage of postponing initialization? If all the above conditions held, there could be no "order of evaluation" effects. Why would the following not be better because run-time (Update: well, 'run-time' is not quite the right term here, but you know what I mean) evaluation is entirely avoided? (In fact, it might even be better than the approach I posted here because you would be dealing with a 'pure' lexical variable without the interpolation problems associated with constant entities. However, methods or functions within the class could still change it, so it could not be considered a 'pure' constant.)
package Foo::Bar;
...
my $regex = qr{ hello }xms;
...
sub new { ... }
...
sub method {
my $self = shift;
...
if ($self->{bar} =~ m{ \b $regex \b }xms) { ... }
...
}
...
1;