isync has asked for the wisdom of the Perl Monks concerning the following question:
vs.package Foo::Bar; use Regexp::Assemble; our $VERSION = '0.01'; sub new { my $class = shift; my $self = bless({@_}, $class); ## precompile regexes $self->{regex} = Regexp::Assemble->new->add( 'some-stuff' )->re; return $self; } sub function { my $self; ... $var =~ $regex; }
package Foo::Bar; use Regexp::Assemble; our $VERSION = '0.01'; our $regex = Regexp::Assemble->new->add( 'some-stuff' )->re; sub function { ... $var =~ $regex; }
I don't hesitate to declare module wide constants within the on-load/BEGIN part of a module, but it somehow feels wrong when these constants require some computation on-load, like here, precompiling regexes.
I would favour the the latter version. Doing a clean OO-style init and having the $obj var around
is a bit cumbersome for the simple module I'm working on.
Arg, noticed that? I used the word "clean" to describe having these 'constants' in $self...
So please Monks, give me some advice. Is it just a matter of style, or design priciples, or are there actual problematic things to consider when my constants do some computing on-load?
(One difference I can think of is that this computing introduces an overhead and with my decision I decide
for the module user where the precompilation overhead ends up:
"fast load" + "mandatory new(), slowed" + "fast method" -- OO-style
vs.
"slower load" + "no mandatory new()" + "fast method". -- on-load style
Anything except that?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Modules: computing a constant, "on load" or in new()?
by BrowserUk (Patriarch) on Aug 20, 2013 at 21:16 UTC | |
Re: Modules: computing a constant, "on load" or in new()?
by tobyink (Canon) on Aug 20, 2013 at 21:56 UTC | |
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 | |
Re: Modules: computing a constant, "on load" or in new()?
by AnomalousMonk (Archbishop) on Aug 20, 2013 at 22:28 UTC | |
Re: Modules: computing a constant, "on load" or in new()?
by isync (Hermit) on Aug 21, 2013 at 00:08 UTC |