Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by bliako (Monsignor)
on Feb 24, 2019 at 13:09 UTC ( [id://1230490]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^7: Importing constans and variables when "require"ing
by soonix (Canon) on Feb 24, 2019 at 18:16 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1230490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found