William G. Davis has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks.

I can't seem to get the constant pragma and HTML::Mason to work together. Putting the "use constant" declarations in <%once> results in seemingly random, sporadic "Constant subroutine ___ redefined" errors, and I also get them if I declare constants in a sub component with the same names as constants declared in the top-level component. Apparently, mod_perl and constant don't always get along.

Does anyone know of any safe ways to declare and use constants with Mason?

  • Comment on HTML::Mason and "use constant" constants?

Replies are listed 'Best First'.
Re: HTML::Mason and "use constant" constants?
by PodMaster (Abbot) on Dec 28, 2003 at 10:37 UTC
    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.

Re: HTML::Mason and "use constant" constants?
by perrin (Chancellor) on Dec 28, 2003 at 21:07 UTC
    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.
Re: HTML::Mason and "use constant" constants?
by simonm (Vicar) on Dec 28, 2003 at 18:26 UTC
    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.

Re: HTML::Mason and "use constant" constants?
by William G. Davis (Friar) on Dec 29, 2003 at 01:30 UTC

    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.

Re: HTML::Mason and "use constant" constants?
by naChoZ (Curate) on Dec 30, 2003 at 14:45 UTC

    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

      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.

        Oh cool. Didn't know about that one. I got the Mason baboon book from Jesse's RT class, but I've only barely cracked the cover.

        --
        Diplomacy is the art of saying "Nice doggie" until you can find a rock.
        naChoZ