Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The documentation for Exporter says this at What Not to Export:


You have been warned already in "Selecting What to Export" to not export: There's one more item to add to this list. Do not export variable names. Just because Exporter lets you do that, it does not mean you should.
@EXPORT_OK = qw($svar @avar %hvar); # DON'T!
Exporting variables is not a good idea. They can change under the hood, provoking horrible effects at-a-distance that are too hard to track and to fix. Trust me: they are not worth it.
perl -MConfig -le 'print scalar keys %Config' 1234

Is it just to maintain backwards compatibility with the good old days?

Replies are listed 'Best First'.
Re: Why does Config.pm break the rules?
by haukex (Archbishop) on Feb 17, 2023 at 14:16 UTC

    These "rules" do have exceptions sometimes. In my view, the point of "use @EXPORT_OK instead of @EXPORT" is to avoid cluttering up the user's namespace, and also to provide a bit more readability: use Config '%Config'; would tell the reader of the code exactly what is being exported, use Config; doesn't. I know some might disagree, but IMHO, some modules are so single-purpose that it's ok to export maybe one or two things by default, if their names won't lead to confusion (e.g. the name of the thing they export is traceable back to the name of the module). IMO, Config is such a single-purpose module, so it's ok to export %Config by default. Also, %Config is read-only, so "[the variables] can change under the hood, provoking horrible effects at-a-distance that are too hard to track and to fix" doesn't apply here either.

    More exceptions might be: some modules exist for the explicit purpose of exporting a bunch of functions (e.g. to reduce boilerplate), and I also think it's sometimes acceptable to export certain variables on request (e.g. in a test script I might provide something like $DEBUG, where the fact that this is a global variable is not a problem).

      I'll second this. Exporting constants in the form of named variables is often quite nice because you can interpolate them into strings, and they don't pollute your method namespace. The only downside is they don't get folded into constant compile-time expressions like happens with constant subs.
Re: Why does Config.pm break the rules?
by tobyink (Canon) on Feb 17, 2023 at 18:11 UTC
      ++tobyink! The perfect quote for this thread, from one of my favorite movies.

      "It's not how hard you work, it's how much you get done."

Re: Why does Config.pm break the rules?
by Corion (Patriarch) on Feb 17, 2023 at 11:36 UTC

    Yes.

Re: Why does Config.pm break the rules?
by LanX (Saint) on Feb 17, 2023 at 14:17 UTC
    Config.pm and sitecustomize.pl are not vanilla modules, but basic infrastructure. They are supposed to provide global vars and configs (sic) when starting.

    That's why they are listed in perlvar

    See Config and perlrun for more

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

Re: Why does Config.pm break the rules?
by Bod (Parson) on Feb 18, 2023 at 02:15 UTC
    Is it just to maintain backwards compatibility...

    I would say not just for backward compatibility

    There are times when global definitions are needed for an environment which will be different in a different environment... For example, we run three different environments for one website. Development, Test and Production. Apart from code that is being written or tested, they are identical except for a configuration file which contains nothing but global variable definitions of things that vary between environments (and comments so we know what they all are!). One such difference is which database schema each environment connects to, another is whether live emails should be sent to users, and so on.

    Config.pm is a similar situation

    For completeness, we don't export the variables. We declare them with our scope and access them with a fully qualified namespace definition. But we could get the same effect by exporting them instead ass all the variables have names that reflect their globalness.