develooper Front page | perl.perl5.porters | Postings from December 2014

Re: [perl #123322] "our" has the same scoping rules as "my" or"state"

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
December 1, 2014 23:49
Subject:
Re: [perl #123322] "our" has the same scoping rules as "my" or"state"
Message ID:
20141201234945.GA15113@plasmasturm.org
* Eric Brine <ikegami@adaelis.com> [2014-11-29 23:55]:
> So sometimes "our" is scoped like "my", and sometimes it isn't? ow!
>
> $ perl -E'use strict; $::x = "pkg";                say our $x, $x'
> pkgpkg
>
> $ perl -E'use strict; $::x = "pkg"; my $x = "lex"; say our $x, $x'
> pkglex
>
> I just said the docs weren't accurate, but now you tell me the
> behaviour is wacky too. The linked thread explains that introducing
> the wackiness was necessary because it was a maintenance release. But
> that's done. I just mentioned the docs were inaccurate, but some
> consistency (one way or another) would be nice.

But it *is* consistent! The `our` is a red herring.

    $ perl -E'$::x = "pkg";                say my $x = "new", $x'
    newpkg

    $ perl -E'$::x = "pkg"; my $x = "lex"; say my $x = "new", $x'
    newlex

Observe that the bare $x in the `say` statement resolves exactly the
same way as in your examples: pkg in the first case, lex in the second.
It resolves precisely the same with `our`. So `our` is not at all wacky
in regards to when the declared alias becomes available.


* Eric Brine <ikegami@adaelis.com> [2014-11-30 02:20]:
> Like "my" and "state", "our" is only visible within a lexical scope.
> Unlike "my" and "state", which both declare new (lexical) variables,
> "our" only creates an alias to an existing variable: a package
> variable of the same name.
>
> When the alias becomes available depends on whether an existing "my",
> "state" or "our" with the same name exists in the current scope. If
> one does, the alias becomes available at the beginning of the next
> statement. Otherwise, it becomes available immediately.

But that’s incorrect. It’s the fact that `our` interacts with strictures
that is wacky.

This would be correct:

  [Same first paragraph]

  Note that the variable name becomes exempt from the vars stricture
  immediately during the same statement. So as long as there is no
  variable with that name is already in scope, you can reference the
  package variable again even during the same statement, despite the
  fact that variable declarations in Perl only take effect for the next
  statement.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About