Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: Importing constans and variables when "require"ing

by soonix (Abbot)
on Feb 24, 2019 at 07:47 UTC ( #1230489=note: print w/replies, xml ) Need Help??


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 (Priest) on Feb 24, 2019 at 08: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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2019-06-25 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (107 votes). Check out past polls.

    Notices?