LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi in the following code does the declaration of $y happen in a different scope.

Is it documented behaviour?

t_set.pl
use strict; use warnings; use B::Deparse; sub test { use set my $y =666; print $y; } #test(); print B::Deparse->new()->coderef2text(\&test);

error
Global symbol "$y" requires explicit package name at d:/exp/t_set.pl a +borted due to compilation errors. ["set", 666]

set.pm
exec 't_set.pl' unless caller; package set; use strict; use warnings; use Data::Dump qw/dd pp/; sub import { dd \@_; } 1;

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re: use has it's own scope?
by Anonymous Monk on Aug 07, 2017 at 22:18 UTC
    use docs say "It is exactly equivalent to BEGIN { require Module; Module->import( LIST ); }" so probably yes LIST has its own scope.

        FYI I wondered if B::Deparse could show this. The answer is yes, with level 5 of deparsing or higher, use statments will be translated into their equivalent BEGIN blocks:

        perl -MO=Deparse,-x5 -e "use Set my $x = 12;" sub BEGIN { require Set; do { 'Set'->import(my $x = 12) }; } -e syntax OK
        I don't think the fact that the lexical has a scope even shorter than the BEGIN block changes anything though.

A reply falls below the community's threshold of quality. You may see it by logging in.