in reply to Re^4: Importing constans and variables when "require"ing
in thread Importing constans and variables when "require"ing

used only once doesn't refer to useing, more to using/defining by "mentioning" it. And since you only require XYZ (the module but not the variable XYZ::PI), you are mentioning XYZ::PI only once. The compilation of XYZ.pm, where XYZ::PI is defined, happens only later, during runtime of example.pl , when the require ist actually executed. If you use XYZ instead, Perl sees the declaration of XYZ::PI early enough to avoid the warning.

Replies are listed 'Best First'.
Re^6: Importing constans and variables when "require"ing
by bliako (Abbot) on Feb 24, 2019 at 13:09 UTC

    OK thanks.

    I will confess that I found it weird that the problem is "possible typo" and not complaining because of unknwon variable. Eventually I paid more attention to the so-frequent error message Global symbol "$x" requires explicit package name (did you forget to declare "my $x"?) at... I realised that it says "requires explicit package name" whereas I have usually been concentrating on "did you forget to declare?" part. Combined with your message and LanX's Another effect of strict (vars) is to enforce pre declaration of unqualified variables with my or our. I get to understand it, hopefully fully.

    So, evidently, one can get away with declaring variables using my/our completely if it uses fully-qualified variables. So use strict; $main::x = 42; will succeed whereas use strict; $x = 42; will fail.

    thanks, bw, bliako

      Yes, but be aware that $main::x is a global variable (as in the first error message), which means it can be accessed and modified from anywhere, even outside the corresponding module. So, if once upon a time its value differs from what you expect, you'll have a huge space to search for the culprit.

      OTOH, if you use scoped (i.e. my) variables, you'll need to search only the correspondig scope.

      The error message
      Global symbol "$x" requires explicit package name
      actually means
      Do you really want "$x" to be a global symbol?