
catalyst at mikeraynham
Jun 11, 2011, 2:15 PM
Post #7 of 10
(1716 views)
Permalink
|
On 11/06/11 21:24, David E. Wheeler wrote: > On Jun 11, 2011, at 11:34 AM, Mike Raynham wrote: > >> My plan is to create an AUTOLOAD function in Bric::Config which will replace each of the constants with a getter function. I've attached a diff of the Bric::Config code that I've been experimenting with so far. >> >> >> if (MEDIA_UNIQUE_FILENAME) { ... } >> >> would be replaced with: >> >> if (Bric::Config::get_media_unique_filename) { ... } >> >> >> The functions could be exported, but I thought it might be clearer (if a little longer) to use the fully qualified name. > > I think you're creating a *lot* of work for yourself that way. Better I think would be to replace all the `use constant` lines in Bric::Config with `sub` lines. Then the interface would be the same, but they could be overridden at runtime. I agree, and simply replacing constants with subs was my first thought. But then I started to think about Perl naming conventions, and it began to feel a bit wrong - even though the subs are essentially doing the same thing as the constants. > >> As there are lots of constants throughout the code, they can be switched out as and when required, rather than all at once. > > Yes, that would be substantially less work, of course. Or substantially less up-front, I should say. I was planning on leaving the bulk of the work to others... ;-) > >> The AUTOLOAD function throws an exception if the configuration directive is defined as a constant, so it shouldn't be possible to use both at the same time. > > Yeah. I still wouldn't use AUTOLOAD, though. Not literally, that is. > >> Many of the constants can simply be removed or commented out, and they will automatically be available via a getter function. In some cases, default values are set at the point at which the constant is created. In these cases, the $config hash value will need setting or updating as appropriate. For example: >> >> use constant MEDIA_URI_ROOT => '/data/media'; >> >> would have to be changed to: >> >> $config->{MEDIA_URI_ROOT} = '/data/media'; >> >> The functions created by AUTOLOAD can then be overridden with Test::MockModule. >> >> Is this what you were thinking? > > No. I would change it to > > eval "sub MEDIA_URI_ROOT { '/data/media' }"; > > Then you get the same result with *not* change to the interface. > >> I've started a DevTest module for Bric::Config too. At the moment it just checks that AUTOLOAD throws the correct exceptions. I'm sure it could be enhanced later. > > (More tests)++. Hell, if you're writing tests, you don't have to do it my way, because your way is more likely to work forever. :-) I'm torn on this one. I think in the long run, AUTOLOAD with getter functions that follow good naming conventions is a clean way to do it. On the other hand, I agree that simply swapping out 'use constant' for subs is a helluva lot simpler. It also means that the code won't be in a state of limbo, with constants in some places, and getter functions in others. What do you mean when you say that my way is more likely to work forever? Regards, Mike
|