in reply to Re^4: Which "use" statements in a module do "bubble up" to the callers?
in thread Which "use" statements in a module do "bubble up" to the callers? [SOLVED]
"use open ..." is lexically scoped as well
This one is a bit tricky, but the answer turns out to be no, use open qw/:std :utf8/; has a global effect.
If you read the documentation again, what it says is "Any two-argument open(), readpipe() (aka qx//) and similar operators found within the lexical scope of this pragma will use the declared defaults." But that's not what we're doing here, we're not trying to affect open calls, instead we're trying to change STD(IN|OUT|ERR), and the documentation about that says "The :std subpragma ... converts the standard filehandles (STDIN, STDOUT, STDERR) to comply with encoding selected for input/output handles" without mentioning lexical scope. So as it turns out, the effect of this pragma is actually global, as this quick test shows:
sub pr { print join(",",@_,PerlIO::get_layers(*STDOUT), "\x{20AC}"),"\n" }
BEGIN { pr(1); } pr(4);
do {
use open qw/:std :utf8/;
BEGIN { pr(2); } pr(5);
};
BEGIN { pr(3); } pr(6);
__END__
Wide character in print at o.pl line 5.
1,unix,perlio,€
2,unix,perlio,utf8,€
3,unix,perlio,utf8,€
4,unix,perlio,utf8,€
5,unix,perlio,utf8,€
6,unix,perlio,utf8,€
That warning comes from the very first pr(1); call, because afterwards the utf8 layer is added to STDOUT, and as you can see it stays active for the rest of the script regardless of lexical scope.
Even if the :std option of the open pragma were to be lexically scoped, what I understand choroba is saying is that if you put the pragma at the top of the main file, it is effectively active for the entire run of the program as well (Update: not quite, see replies).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Which "use" statements in a module do "bubble up" to the callers?
by Nocturnus (Scribe) on Sep 02, 2017 at 10:04 UTC | |
by haukex (Archbishop) on Sep 02, 2017 at 10:56 UTC | |
by Nocturnus (Scribe) on Sep 02, 2017 at 13:51 UTC |