[Wellington-pm] 'my' and 'our'

Sam Vilain sam at vilain.net
Wed May 3 16:02:04 PDT 2006


Cliff Pratt wrote:

>So, if the variable ($bar) is not used outside the package, it can be 
>declared with 'my'. If it is defined with 'our' it is available anywhere 
>in the package (or scope) as simply $bar
>  
>

"Scope" is correct. If you declare a variable with "our" inside a sub,
for instance, you will need to declare it in other subs in the same
package to refer to it.

ie:

use strict;

sub foo {
our $bar;
}

sub baz {
$bar; # error
}

sub frop {
our $bar; # same $bar as in &foo
}

>Since all object variables should be accessed by an accessor method, in 
>an object situation, the use of 'our' should be rare.
>  
>

Indeed. It's generally considered bad form, especially where it can be
easily avoided.

>Similarly in an ordinary package, you wouldn't normally expose a package 
>variable unless you had a good reason, probably to configure something 
>basic in the package.
>  
>

Yes, and if you're writing OO properly, it's usually more appropriate to
use "class methods" for those. ie, people need to write "ClassName->foo"
to get the variables.

Sam.

>Am I correct?
>
>Cheers,
>
>Cliff
>
>  
>



More information about the Wellington-pm mailing list