in reply to Re: 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)? Also adding a sub in XYZ and calling it from main works ok. Is it because it can live without unresolved subs until it actually has to call them? But it has to resolve each and every variable before execution (when using strict at least)?

The long story is that I ended using require because I have a module which overrides a core sub (sleep) via a BEGIN block. I need that to apply for ALL subsequent modules, mine or foreign, and I kind of reasoned that loading everything else at runtime (via require) will make sure that this is satisfied and sleep is overriden. Now I am not sure whether a module used via another module is using the overriden sleep version.

Replies are listed 'Best First'.
Re^3: Importing constans and variables when "require"ing
by LanX (Saint) on Feb 23, 2019 at 14:32 UTC
    > 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

      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.
        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.

Re^3: Importing constans and variables when "require"ing
by BillKSmith (Monsignor) on Feb 22, 2019 at 23:10 UTC

      You are right, that previous post has many gem answers. I am just not absolutely sure that they can work in ALL situations where modules load other modules etc. My conclusion was

      you must override/alias your sleep() BEFORE use statements for any modules which will use it
      and right now /with this post I am trying to make sure that the "BEFORE" is ensured 100%.

      The most bullet-proof way I guessed it was to override at compile time and then require all modules which happens at runtime, BEFORE is ensured 100%. However, that reveals side-effects. So I have to write tests for my specific case to ensure that BEFORE ... Thanks (and for your replies in the previous post).

      For the record that works and should work as long as it is use'd before any modules whose their sleep needs to be overriden (from LanX's answer at Copy a builtin sub to a different name and then override):

      package Override::Sleep; # based on LanX's answer at https://perlmonks.org/?node_id=1215668 our $total_sleep_time = 0; our $DEBUG = 0; BEGIN { my $oldsleep = \&CORE::sleep; *CORE::GLOBAL::sleep = sub(;$) { #$Override::Sleep::total_sleep_time += CORE::sleep($_[0]); $Override::Sleep::total_sleep_time += $oldsleep->($_[0]); if( $Override::Sleep::DEBUG ){ my $parent = ( caller(1) )[3] || "N/A"; print 'CORE::GLOBAL::sleep('.$_[0].") (called by $parent): + total sleep time is now ".$Override::Sleep::total_sleep_time." secon +ds.\n"; } }; } 1;