in reply to Modules: computing a constant, "on load" or in new()?
Personally I'd do something like this:
package Foo::Bar; use Regexp::Assemble; our $VERSION = '0.01'; my $regex; sub new { my $class = shift; my $self = bless({@_}, $class); return $self; } sub function { my $self; ... $regex ||= Regexp::Assemble->new->add( 'some-stuff' )->re; $var =~ $regex; }
That is, delay the compilation to the last possible moment, and then keep the result for future invocations to use.
Type::Tiny does this sort of thing all over the place, and it's fast. :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Modules: computing a constant, "on load" or in new()?
by AnomalousMonk (Archbishop) on Aug 20, 2013 at 22:56 UTC | |
by tobyink (Canon) on Aug 21, 2013 at 07:00 UTC | |
by AnomalousMonk (Archbishop) on Sep 08, 2013 at 04:17 UTC |