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

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

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


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

Thanks for the added clarifications. Being quite lazy I never searched into the manuals for what a Perl constant is. I just assumed it would be the same as C. And that's why I use it often: more for (thinking) being lighter than a variable and much less for its read-only property. I did see smoke signals in forums saying that they never really use Perl constants, there is no point, but I ignored them. And so a constant is a sub with constant return value which ideally would be optimised by Perl to become more-or-less C #define.

That also explains the fact that there is no complain if I replace what Perl sees as a bareword "XYZ::CONST" with its sub equivalent "XYZ::CONST()" something like what Perlbotics suggested but without the use const.

So a Perl constant is a glorious sub and would be lighter than a variable if Perl eventually optimises it to a constant realising that it has a constant return value. And it's read-only. Great!

Now, why does it complain with Name "XYZ::PI" used only once: possible typo when I print the foreign variable (print $XYZ::PI). Doesn't the definition of "use" include write AS WELL as READ?

bw, bliako

Replies are listed 'Best First'.
Re^5: Importing constans and variables when "require"ing
by soonix (Canon) on Feb 24, 2019 at 12:47 UTC
    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.

      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?
Re^5: Importing constans and variables when "require"ing
by LanX (Saint) on Feb 24, 2019 at 15:09 UTC
    In short: Strict and warnings add a lot of compile time checks to help you catch "potential" problems.

    This usually works well if you don't try to tweak the normal procedures like you do by switching to runtime effects.

    Either ignore or disable these messages or start to read what Perl really does. ;)

    Hint:

    Perl 5 is mostly compatible to Perl 4 which didn't have strict ...

    edit

    ... and does dynamic binding of vars and subs unlike C.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^5: Importing constants and variables when "require"ing
by Your Mother (Archbishop) on Feb 24, 2019 at 16:42 UTC

    Readonly might be closer to what you’d imagine you’d like. Check the whole doc though.

      I am reading it right now and finding lots more of caveats regarding constants...

Log In?
Username:
Password:

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

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

    No recent polls found