1

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"?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Kev
  • 15,899
  • 15
  • 79
  • 112

3 Answers3

5

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

See the thread on Perlmonks for more details.

Mr. Muskrat
  • 22,772
  • 3
  • 20
  • 21
  • 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 Feb 03 '09 at 20:48
  • 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 Feb 05 '09 at 15:38
  • That's a very good question. I know that I have seen threads about it on the various lists... – Mr. Muskrat Feb 06 '09 at 18:45
1

You definitely need my.

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

innaM
  • 47,505
  • 4
  • 67
  • 87
0

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

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Kev
  • 15,899
  • 15
  • 79
  • 112
  • Add this to the question. Then remove this "Answer". – Brad Gilbert Feb 03 '09 at 18:32
  • 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 Feb 03 '09 at 20:38
  • 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 Feb 05 '09 at 14:05
  • 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 Feb 05 '09 at 15:14
  • 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 Feb 05 '09 at 16:06