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.
| [reply] |
|
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
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
|
|
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.
| [reply] |
|
| [reply] |
|
Re^3: Importing constans and variables when "require"ing
by BillKSmith (Monsignor) on Feb 22, 2019 at 23:10 UTC
|
| [reply] |
|
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). | [reply] |
|
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;
| [reply] [d/l] [select] |