Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

use has it's own scope?

by LanX (Saint)
on Aug 07, 2017 at 21:26 UTC ( [id://1196928]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1196928]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found