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

Re^3: use has it's own scope?

by sundialsvc4 (Abbot)
on Aug 08, 2017 at 14:51 UTC ( #1196994=note: print w/replies, xml ) Need Help??

in reply to Re^2: use has it's own scope?
in thread use has it's own scope?

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.

Replies are listed 'Best First'.
Re^4: use has it's own scope?
by chromatic (Archbishop) on Aug 08, 2017 at 15:27 UTC
    Even though it is correct

    It is not correct. Your examples prove that you're wrong.

    strict's error message is that strict doesn't understand the LIST arguments. Perl understands them just fine. The set error message is that Perl didn't find a module. Perl understood the LIST arguments just fine.

    Please stop posting nonsense. You're wasting time and hurting people who don't know enough to ignore your ignorance.

    Update: I reread this and realized the nonsense went even deeper.

    The intended declaration of $y did not succeed

    Indeed it did. That's why strict complains about the unrecognized tag '666'.

    There is probably a “hole” in the BNF grammar within the interpreter.

    Utter nonsense, which proves you didn't bother to look at the grammar at all.

    First of all, there's no "BNF grammar" in the interpreter. There's a parser and a lexer which you could, if you were enterprising enough, potentially represent in Backus-Naur Form, but it's not expressed as such anywhere within the Perl source code, and there is no singular representation of grammar within the Perl source.

    Second of all, you're expressing surprise that assignment is an expression. In other words, by your logic, the fact that $x = $y = $z works is an unanticipated accident of implementation that should be fixed. Similarly, (my $x = $y) =~ s/\A\s+|\s+\Z//g; should not work, in your incorrect interpretation of Perl.

    Arguably, you could be complaining that lexical declaration should not be allowed in expressions, which is still a silly point given the en-passant assignment and substitution technique of the previous snippet, but witness:

    sub main { foo(my $x = 1); say $x; return 0; } sub foo { say ++$_[0] . ' in foo'; }

    ... or:

    sub main { bar(my $x); say $x; return 0; } sub bar { $_[0] = 'assigned in bar'; }

    Should you write code like that? I think we all know the answer—but let's not delude ourselves into pretending that this is some unintentional side effect of implementation. Assignment-as-expression semantics, along with compile time variable declaration, are well established in Perl. Misunderstand them at your own peril.

      Oh, man. When chromatic slaps you down, you know you're past due for some serious introspection.

        Oh, man. When chromatic slaps you down, you know you're past due for some serious introspection.

        obvious self deception is obvious

      Simpler examples of declaration inside of an expression:

      open(my $fh, ...)
      ( my $x = $y ) =~ s/.../.../;
Re^4: use has it's own scope?
by Anonymous Monk on Aug 08, 2017 at 15:21 UTC
    If you had actually read the original post, you might have noticed that the OP did, in fact, create a module called The syntax use set my $y =666; is actually valid, but it's also useless because $y goes out of scope immediately. Arguably it should be disallowed, but nobody cares.
      Arguably it should be disallowed

      Why? LIST here is the standard argument passing mechanism. Making an exception for a my expression in an argument list or making an exception for expressions in import lists would be complicated and break symmetry of syntax, semantics, and implementation.

      This is the piece that sundial's missing. It's a confusing combination of existing Perl behavior because it's uncommon and relatively useless, but it's valid syntax because of the way Perl works.

        Actually, I agree with you -- I didn't say I would be the one to make that argument ;-)

        Maybe I should have suggested that it could arguably be a warning. Perl warns on all kinds of things that are valid but "potentially confusing," and there seems to be a fair amount of back-and-forth about what those things are.

Re^4: use has it's own scope?
by Anonymous Monk on Aug 08, 2017 at 14:59 UTC
    Frankly my dear, you're full of it. Stop posting crap.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1196994]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2019-07-24 03:02 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (32 votes). Check out past polls.