in reply to Re^2: Closures and scope of lexicals
in thread Closures and scope of lexicals

But it's supposed to be file-scoped. $i is scoped to the file in the OP's code. Your suggested change would make the code different than the OP's code.

Replies are listed 'Best First'.
Re^4: Closures and scope of lexicals
by LanX (Saint) on Oct 31, 2024 at 14:31 UTC
    I was referring to his first example which prints 123

    That's more complicated to achieve in Python IMHO.

    Edit

    I think JS had a similar effect with var , that's why let was introduced

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      I didn't translate the first snippet. The question is about the second snippet. The OP asked if its behaviour is a quirk. I showed that it's not by showing that the behaviour is consistent across many languages.

      Yes, JS's var is function-scoped at the narrowest. It's even retroactive such that

      foo = 1; var foo;
      is equivalent to
      var foo; foo = 1;