views:

115

answers:

3

To tailor your scripts toward mp2, avoiding the need for any compatibility wrappers and such, it's said that you're supposed to declare variables using "local our" rather than "my". What about in modules?

sub new
{
    local our $type = shift;
    local our $self = {};
    bless $self, $type;
}

Is that right? Or should it be 'my' so the rest of the module can get at $self under "use strict"?

+1  A: 

You definitely need my.

The local our advice pertains to variables that are global in your module.

innaM
So all along, the moral of the story should've been, globals are evil?
Kev
A: 

Also $self is obtained in methods as $_[0] (Perl automatically prepends @_ with it.)

Kev
Add this to the question. Then remove this "Answer".
Brad Gilbert
It's an answer to 'Or should it be 'my' so the rest of the module can get at $self under "use strict"?' that I discovered after asking.
Kev
Please, don't use $_[0]. I recommend never doing that, but there may be exceptions. Don't use it for the sake of mod_perl anyway.
innaM
I just meant it was the first element. You could use shift. But, I'm curious, why do you say that? Isn't it more efficient than shift? You haven't said what downside motivates your recommendation.
Kev
Oh. I thought that was obvious: 1. It's ugly. very ugly. 2. It's hard to type. 3. It's hard to read (is this a method and is $_[0] thus self? Or is this something else entirely). 4. It may trigger strange little bugs when another method changes @_.
innaM
Oh, okay.
Kev
+4  A: 

local our is an ugly construct that will bite you in the long run.

See the thread on Perlmonks for more details.

Mr. Muskrat
I couldn't fully grasp the discussion that followed; however, the guide still does include it two years later, so perhaps he didn't win the discussion list argument?
Kev
As it turns out, even Stas Bekman agrees ( http://www.nabble.com/local-our-td6489165.html )...I wonder why the docs were never updated after all that? :|
Kev
That's a very good question. I know that I have seen threads about it on the various lists...
Mr. Muskrat