in reply to Re^5: Holding site variables
in thread Holding site variables

inside a package so they are properly namespace

Yes - they are in a namespace with package. Sorry, I forgot that bit as I typed the example instead of copying and pasting as I don't want to reveal actual names for security reasons.

use constants for items of data which won't change during a run

None of this configuration will change during a run, so there's a case for constants for all of it.

I just never think to use constants...not sure why!
Could you explain what the advantage here is in using a constant?

I understand that they can make it clearer that they will be constant and that the avoid "magic numbers". But in this case every variable in this namespace is constant so clarity is not an issue IMHO

Replies are listed 'Best First'.
Re^7: Holding site variables
by hippo (Archbishop) on Mar 21, 2024 at 22:31 UTC
    Could you explain what the advantage here is in using a constant?

    Sure - it's the most obvious advantage: you won't be able to change them accidentally. If you do so it will give a compile-time error. Here is my previous example with just one extra line to demonstrate:

    #!/usr/bin/env perl use strict; use warnings; package MyStuff { use constant BODPLAN => 'Have a separate dev server'; # ... more constants here } print "Bod's plan: " . MyStuff::BODPLAN . "\n"; MyStuff::BODPLAN = 'Use globals everywhere!';

    If you try to run that (or even compile it) it will error out. If you don't use constants you will not get that level of protection.


    🦛