in reply to Re^2: Is this a bug in Perl scope?
in thread Is this a bug in Perl scope?

our and my are very different things. our creates an alias for a global variable. my creates a name and a value.

The only thing the two have in common is that the alias created by our and the name created by my are valid within the current scope.

As Perl assumes that you don't want to shadow the name created by a my statement, because it likely is the only way to get at its associated value, it warns you about this.

I don't know why our doesn't always warn, but that may be because I don't use our. I prefer the vars pragma, which only creates the values within the package, which seems to me what you expect.

I think the main issue for you is that package does not end a previous lexical scope. Some people try to manage that by using the following syntax:

package foo { # this block provides lexical scope for package foo ... }; package bar {# this block provides lexical scope for package bar ... };

I don't see much use in that because my packages are either so large that they live in separate files or are so tiny that they only consist of configuration data masquerading as code. In neither case, lexical scope plays much of a role on the package scope.

Replies are listed 'Best First'.
Re^4: Is this a bug in Perl scope?
by tobyink (Canon) on Jul 22, 2012 at 19:15 UTC

    This syntax:

    package foo { ... }

    ... only works in Perl 5.14 and above. In earlier versions of Perl, you'll need a semicolon before the block...

    package foo; { ... }

    Though it is in my experience more conventional to put the package declaration inside the block itself:

    { package foo; ... }

    Update: as per AnomalousMonk's post below, this latter style has the added advantage that the package declaration itself is lexically scoped, so after perl parses the closing curly brace it puts you back into the main package. I knew that already really. ;-) The new Perl 5.14 syntax also does this, but the middle example does not.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      { package foo; ... }

      In addition, prior to 5.14 (and even post-5.14) this idiom terminated the scope of the enclosed package, which was (and is) very useful.

      >perl -wMstrict -le "{ package Foo; sub my_pkg { print 'works pre-5.14: ', __PACKAGE__; } } ;; package Bar { sub my_pkg { print 'post-5.14: ', __PACKAGE__; } } Foo::my_pkg(); Bar::my_pkg(); print __PACKAGE__; " works pre-5.14: Foo post-5.14: Bar main

      Update: Expanded code example.