Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by LanX (Saint)
on Feb 23, 2019 at 14:32 UTC ( [id://1230469]=note: print w/replies, xml ) Need Help??


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

> Fair enough, but why does it work and print the value of $PI if I comment out the use of CONST in main entirely (i.e. main's last line)?

Strict has 3 effects, one (subs) is to restrict the use of barewords to only known subs. Constants are implemented as subs and need to be known at compile time.

See also "use subs "

This doesn't necessarily mean that the sub known at compile time is also the called sub, because resolution happens at run time. (Keep in mind that you can redefine subs dynamically, this will just update the reference in the symbol table)

Any sub called with & or trailing () is not a bareword anymore, and not effected by strict.

Another effect of strict (vars) is to enforce pre declaration of unqualified variables with my or our.

But your variables are fully qualified. And barewords are not variables (under strict).

Update

It all becomes much easier if you first understand how Perl works without strict, an why these restrictions need to be done.

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

  • Comment on Re^3: Importing constans and variables when "require"ing

Replies are listed 'Best First'.
Re^4: Importing constans and variables when "require"ing
by bliako (Monsignor) on Feb 24, 2019 at 12:20 UTC

    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

      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

      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

      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://1230469]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found