Does anyone know of any safe ways to declare and use constants with Mason?
- just use them, cause those sporadic warnings are not so sopradic (if mason/mod_perl/whoever reloads you component for whatever reason (your component changes...), of course those constants are going to be redefined -- this is expected (under a persistant environment like mod_perl)).
- you could use Readonly (if that's why you're using constants)
- Don't use constants (unless a module exports constants, I don't see how they're of much use).
| MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!" | | I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README). | | ** The third rule of perl club is a statement of fact: pod is sexy. |
| [reply] |
Your problem comes from the fact that all Mason components are in the same package. Constants are just subroutines, so if you follow the same rules you would with using globals in Mason you should be fine. In my opinion, using the constant pragma is just begging for trouble, and you'd be better off using normal globals. | [reply] |
Does anyone know of any safe ways to declare and use constants with Mason?
Give them unique names, and place them in a separate file that does not get reloaded frequently, such as a new My_Project::Constants package.
| [reply] |
Thanks for all of the helpful replies, everyone. I was using the constants to give names to magic numbers I was passing in query string parameters. E.g.:
something?action=1
...
<%once>
use constant LOGIN => 1;
use constant LOGOUT => 2;
</%once>
<%init>
if ($action eq LOGIN) {
...
I think I'll replace the numbers passed in the parameter values with their constant-name equivalent. E.g.:
something?action=login
instead.
| [reply] [d/l] |
I believe the proper place for would be in the <%ARGS> section. For instance, in one of my custom RT forms, my ARGS section is:
<%ARGS>
$DependsOn => undef
$DependedOnBy => undef
$MemberOf => undef
$QuoteTransaction => undef
$Queue => 35
</%ARGS>
--
Diplomacy is the art of saying "Nice doggie" until you can find a rock.
naChoZ
| [reply] [d/l] |
Interesting, but in a top-level component, <%args> can and is supposed to be overwritten by query string parameters.
I think <%attr> is much better place for these constant-type things, though, since <%attr> is evaluated only once when the component is loaded for the first time and then persists for the life of the child process, and the attributes themselves can't be modified at all after that. Also, component attributes are usually universally accessible even from sub components via $m->base_comp->attr(), saving you from having to use costly <%shared> blocks, touching caller_args(), or passing values explicitly from component to component.
| [reply] |
| [reply] |