There seems to be some confusion regarding the term 'declaration' in perl. The term seems to be overloaded in a number of ways that are not entirely consistent. By this I mean that the term is overused, in places where its usage does resonate with its common usage in other languages. Generally speaking I think people consider statements like
my $foo; sub bar {}
to be declarations. They declare that a given object is to be created and henceforth will be reffered to by a given name. But what about statements like
are they declarations? Even though many people will say "the package declaration" or the "our declaration" in conversation IMHO they arent declarations at all. They dont "create" in the sense that the my or sub declarations do. In reality its safer to think of them as compiler pragmas than anything else. package simply changes the way perl parses unqualified identifiers within the given lexical scope, and our or use vars simply change the way strict behaves when perl encounters an unqualified identifier within the given lexical scope.package Foo; our $Bar;
At first glance the distinction is fuzzy and unclear. But maintaining the distinction is the only way that I personally can keep the subtly different behaviours of the two different groups of keywords. Consider the following code
my $foo=1; # create an anonymous lexical and label it as $foo { my $foo=2; # create an new anonymous lexical and also label it $fo +o # this $foo will block the other $foo from being access +ed # by name until we reach the end of scope print $foo,$/; } print $foo,$/; #prints 2 and 1; #--------------- our $Foo=1; # Tell Perl that its ok for us to access $Foo # without qualification, and give it 1 as a value. { our $Foo=2; # Tell Perl that its ok for us to access $Foo # without qualification, and give it 2 as a value. print $Foo,$/; } print $Foo,$/; # prints 2 and 2
In this case its clear the behaviour is different. Likewise the behaviour of package is totally different from a lexical or subrotuine declaration. If we play around with the four keywords we see they do indeed behave quie differently, as the following code shows
# thess pragmata apply to everything below use strict; use warnings; package Foo; my $Bar; sub Foo{} package Bar; my $Bar; sub Bar{} sub Foo::Bar{} package Foo; sub Baz{} sub Bar{} __END__ "my" variable $Bar masks earlier declaration in same scope at D:\perl\ +devlib\Text\strict2.pl line 7. Subroutine Bar redefined at D:\perl\devlib\Text\strict2.pl line 12.
We see that my $Bar doesnt create a $Bar in each package, but rather the $Bar is available to all code in its scope regardless of package name. Furthermore we see that duplicating a subroutine or lexical declaration results in a warning. Having multiple package statments does not. Multiple declarations result in the older declaration being changed or unavailable for a period of time. Multiple use of our or package has no such side effects.
Ultimately I think its very important not to consider package and our (nor local) to be declarations. They aren't. And if you think they are eventually you will get tripped up. Thinking of sub and my as declarative operators, and our and package as compiler pragmatta provides two simple models that explain the subtly different behaviour in a simple and coherent way. Which I personally find reduces confusion.
NOTE If you dont agree with my use of the word declaration, then please suggest another term. My use of it as I have comes from other languages such as C, and Pascal. Also I should mention that I deliberatly avoided the declaration/definition/initialization distinction in this note. Im fully aware that these need not happen at the same time or same place, and that my use of declaration more or less rolls them up into one. I dont think this detracts from the core point however.
Cheers, and hope this helps somebody,
In reply to On Declaration by demerphq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |