in reply to How relevant is the order of 'use's ?
It seems to come from the way constant works.
So constants aren't actually just functions that are added to the symbols table, because instead of a glob there is a reference to the value.use Data::Dump qw/pp/; use constant { C1 => 10 }; sub C2 { 20 } say pp $::{C1}; say pp $::{C2}; __DATA__ \10 *main::C2
The problem with your code is that your import function replaces the whole entry in the symbols table (everything in $::{SUCCESS} is replaced by $Demo2::{SUCCESS}), instead of just the CODE section of the glob. Somehow it seems that constant makes the reference to the constant's value and the glob coexist, but when you remove the glob manually from the symbols table, perl can't find the symbol SUCCESS anymore.
Funny thing is, if you just remove the line that prints the value of the constant SUCCESS, $SUCCESS gets printed, but with the constant's value. So with : use constant { SUCCESS => 5 }; in Demo2, the line print "\$SUCCESS: $SUCCESS\n"; prints "SUCCESS: 5".
Something like $main::{SUCCESS} = $Demo2::{SUCCESS}->*{CODE} (under use feature 'postderef') may work better. (Edit: no it doesn't, since $Demo2::{SUCCESS} is a ref to a scalar, not a glob...)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How relevant is the order of 'use's ?
by Anonymous Monk on Oct 20, 2016 at 14:21 UTC | |
by Eily (Monsignor) on Oct 20, 2016 at 14:38 UTC | |
Re^2: How relevant is the order of 'use's ?
by Krambambuli (Curate) on Oct 20, 2016 at 13:58 UTC | |
by Eily (Monsignor) on Oct 20, 2016 at 14:27 UTC |