Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

use has it's own scope?

by LanX (Bishop)
on Aug 07, 2017 at 21:26 UTC ( #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?
use strict; use warnings; use B::Deparse; sub test { use set my $y =666; print $y; } #test(); print B::Deparse->new()->coderef2text(\&test);

Global symbol "$y" requires explicit package name at d:/exp/ a +borted due to compilation errors. ["set", 666]
exec '' 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.

Re: use has it's own scope?
by sundialsvc4 (Abbot) on Aug 08, 2017 at 03:03 UTC

    I must yield to the greater experience of Other Monks on this point, but the syntax use set my $y =666; is quite unfamiliar to me.   To my reading, it appears to be syntactically invalid such that I am not sure why Perl did not complain vigorously.

    Nevertheless, a quick review of use will clearly show that use “serves a quite-remarkable number of different (and unrelated) purposes in this language.”   So it goes ... and ...

    package will also serve to indicate that package might not do exactly what you think it does, either.

    - - - - -

    use can either serve as what other languages would refer to as a pragma, or it may serve as a reference to an external module.   Although the syntax seems to be exactly the same, the meaning (and scope) is in fact entirely different.   I-f(!) the statement declaration/pragma is understood to be a declaration of a reference to an external module, then the necessary compiler action will occur at BEGIN{} time ... i.e. before anything else happens.

    package, on the other hand, has a truly-semantic meaning:   it introduces a new package-name (which is only of interest to things which care about package-names in the first place ...), from that point forward.

    In the case at bar, I rather suspect that Perl didn’t understand use set my $y =666; at all, but instead failed to pick up on your probable intention to declare my $y =666;.   I would guess that it failed to realize that you intended for $y to be a local variable, and complained accordingly ... perhaps for the lack of a semicolon.   As I said earlier, I am surprised that Perl accepted this construction at all, and I await the clarification of Other Monks as to why exactly this was so.

      "... the syntax ... is quite unfamiliar to me."

      The fix for that would be to learn the language and familiarise yourself with the syntax.

      "I rather suspect that Perl didnít understand use set my $y =666; at all, ..."

      You could have just run some very simple tests to determine the validity of that.

      $ perl -Mwarnings -Mstrict -E 'say "@INC"' /some/path /other/path ... $ perl -Mwarnings -Mstrict -E 'use lib; say "@INC"' /some/path /other/path ... $ perl -Mwarnings -Mstrict -E 'use lib my $x = "fred"; say "@INC"' fred /some/path /other/path ...

      I've downvoted your post.

      — Ken

        Of course you did, and so did everybody else.   But Rhett Butler had something to say about that, at the end of a very famous movie.

        Now, if you are curious about what Perl does do with the “unfamiliar” syntax ...

        $ perl -e 'use set my $y = 666;' Can't locate in @INC (you may need to install the set module) ( +@INC contains: ...) BEGIN failed--compilation aborted at -e line 1. $ perl -e 'use strict my $y = 666;' Unknown 'strict' tag(s) '666' at -e line 1. BEGIN failed--compilation aborted at -e line 1. $ perl -e 'use DBI my $y = 666;' DBI version 666 required--this is only version 1.631 at ... $ perl -e 'use FileCache my $y = 666; print "It is $y not here\n";' It is not here

        So, as I didn’t bother to say because I shouldn’t have to ... but maybe nothing gets heard around here unless it is accompanied by a code sample ... the syntax that is “unfamiliar to me” is so, because it is not valid at all.   The intended declaration of $y did not succeed, and also did not produce a syntax error as it should have.   (There is probably a “hole” in the BNF grammar within the interpreter.)

        And here, by the way, is the “semicolon” that I spoke of, which breaks the line into two syntactically-valid statements:

        $ perl -e 'use FileCache; my $y = 666; print "It is $y not here\n";' It is 666 not here

        Now, be good boys and girls and hurry up and downvote this one, too, “just because sundialsvc4 said it.”   Even though it is correct, as was (as you now see) my previous post, as well.

      > "is quite unfamiliar to me"
      Because it's not lies, abuse or racism, it's Perl.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1196928]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2018-06-24 19:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.