All I can say is: Good luck. Have fun.
:) Zarabozo, Francisco (GE, Corporate) wrote: > Thank you for your answer. > > Yes, I thought so too. Constants are by definition not-variables. I wanted > to know this for the following reasons: > > 1. All you know how a company asks you to change the less code possible > whenever something is requested in critical applications. > 2. I didn't desing the modules. The person who did it thought that using > constants would be nice and he didn't think about something like this > coming. > 3. This module is superclass for other 15 modules that use its constants. > > So, I will have to modify such constants in all 16 modules to convert them > in variables in order to make them configurable. I guess It's always better > a clean long fix than a dirty short patch. :-) > > Thank you again. > > Cheers, > > Paco Zarabozo > > > > > > From: Brian Raven > Sent: Wednesday, March 05, 2008 8:10 AM > To: [email protected] > Subject: RE: Redefining Constants > > > Zarabozo, Francisco (GE, Corporate) <> wrote: > >> Hello All, >> >> I'm having some trouble with a module where I need to implement a >> change. >> Basically, this module defines constants that other modules will use >> at a later time. >> >> I need to detect parameters in the import() sub (that's the requested >> change) and, if the case, redefine some of those constants. >> >> I'm trying with evals but all I'm getting is a warning about >> "constant redefined", AND the constant is not really changing (it >> keeps its previous value). >> > > No. It will be complaining about a subroutine being redefined. > > >> Is there a way to correctly redefine a constant at import() time? >> > > I would hope not! That would subvert the whole concept of a constant. > However, ... > > >> Here is a simple example of the scene: >> >> Script.pl >> ----------------- >> use strict; >> use warnings; >> use myModule constantA => 'foo'; >> ----------------- >> >> myModule.pm >> ----------------- >> package myModule; >> use strict; >> use warnings; >> use constant constantA => 'bar'; >> >> sub import { >> my %params = @_; >> if ($params{constantA}) { >> # This should change constantA from 'bar' to 'foo' >> eval 'use constant constantA => \''.$params{constantA}.'\''; >> warn $@ if $@; >> } >> } >> ----------------- >> >> Any comments are highly appreciated. >> > > While this appears to do what you want (at least it does after > modification), the warning about redefinition of a subroutine may be > hard to inhibit without changing constant.pm. One way would be to avoid > redefining any subroutines, e.g. > > ------------------------ > use strict; > use warnings; > package myModule; > > our %defaults = (constantA => 'bar'); > > sub import { > my $module = shift; > my %params = @_; > foreach my $k (keys %defaults) { > my $val = defined($params{$k}) ? $params{$k} : $defaults{$k}; > eval qq{use constant $k => '$val'}; > warn $@ if $@; > } > } > > 1; > -------------------------- > > Its not entirely clear what you are trying to achieve, but I don't think > that variable constants (i.e. constants that are not constant) is a good > way to do it. > > HTH > > _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
