95

I keep seeing the "my" keyword in front of variable names in example Perl scripts online but I have no idea what it means. I tried reading the manual pages and other sites online but I'm having difficulty discerning what it is for given the difference between how I see it used and the manual.

For example, its used to get the length of the array in this post: Find size of an array in Perl

But the manual says:

A my declares the listed variables to be local (lexically) to the enclosing block, file, or eval. If more than one value is listed, the list must be placed in parentheses.

What does it do and how is it used?

Community
  • 1
  • 1
FistOfFury
  • 5,598
  • 5
  • 43
  • 55
  • 3
    Re "its used to get the length of the array in this post", Not at all. `my` did not factor into it in the least. It's the scalar assignment operator (`=`) that enforced the scalar context on `@arr`. – ikegami Jan 02 '14 at 20:03
  • If it's not really "yours", you should not use it. – icenac Jan 23 '17 at 14:26

3 Answers3

148

my restricts the scope of a variable. The scope of a variable is where it can be seen. Reducing a variable's scope to where the variable is needed is a fundamental aspect of good programming. It makes the code more readable and less error-prone, and results in a slew of derived benefits.

If you don't declare a variable using my, a global variable will be created instead. This is to be avoided. Using use strict; tells Perl you want to be prevented from implicitly creating global variables, which is why you should always use use strict; (and use warnings;) in your programs.


Related reading: Why use use strict; and use warnings;?

Community
  • 1
  • 1
ikegami
  • 322,729
  • 15
  • 228
  • 466
  • 13
    Finally an answer that explains *why*. – ThisSuitIsBlackNot Jan 02 '14 at 21:18
  • For people coming here from JS: It's essentially like `var` and `let` in JavaScript. Without `'use strict';` JS will let you declare new global variables without either keyword, which isn't considered the best of practices. – Electric Coffee Aug 10 '20 at 07:25
  • @Electric Coffee, In JS terms, the question would be "What does `let` and `var` do and how should they be used?" And yes, the answer would be roughly the same. (JS's `let` vars are a lot like Perl's `my` vars, except that `let` vars can't be captured.) – ikegami Aug 10 '20 at 07:52
  • 1
    I just meant that if people came to Perl from JS, it would be an easy way to explain the same concept – Electric Coffee Aug 10 '20 at 08:32
  • @ElectricCoffee 1) A programmer that understands scoping doesn't need to have it explained to them again after switching languages. 2) Not sure that you actually did explain scoping, in terms a JS programmer would understand or otherwise. Maybe you were trying to explain `use strict;`? But that wasn't the question, and `use strict;` does more than that. Or maybe you meant to communicate that `my` limits the scope of vars kinda like `let` and `var` do, but that's pretty much the first sentence of the answer. 3) If you wish to answer a question differently, that's what Answers are for. – ikegami Aug 10 '20 at 08:52
29

Quick summary: my creates a new variable, local temporarily amends the value of a variable

In the example below, $::a refers to $a in the 'global' namespace.

$a = 3.14159;
{
  my $a = 3;
  print "In block, \$a = $a\n";
  print "In block, \$::a = $::a\n";
}
print "Outside block, \$a = $a\n";
print "Outside block, \$::a = $::a\n";

# This outputs
In block, $a = 3
In block, $::a = 3.14159
Outside block, $a = 3.14159
Outside block, $::a = 3.14159

ie, local temporarily changes the value of the variable, but only within the scope it exists in.

Source: http://www.perlmonks.org/?node_id=94007

Update

About difference between our and my please see

(Thanks to ThisSuitIsBlackNot).

Community
  • 1
  • 1
Igor Chubin
  • 51,940
  • 8
  • 108
  • 128
  • 1
    I think this answer needs some adjusting. You have a code example demonstrating the use of `my`, but then follow that up with a sentence talking about `local` as if you had just demonstrated `local`'s use, despite that `local` is not demonstrated anywhere in this answer. I think you kind of misquoted the source on PerlMonks, which has two code examples. The sentence about `local` was describing the other one, not the one you transcribed here. – temporary_user_name May 17 '17 at 08:01
  • Why even mention `local` when you don't use it in the code example? – Greenonline Dec 16 '20 at 15:48
  • @Greenonline: With this logic, if you have no code example at all, you shouldn't mention anything? – Igor Chubin Dec 16 '20 at 21:31
  • Well yes. As mentioned in [this comment](https://stackoverflow.com/questions/20889609/how-should-i-use-the-my-keyword-in-perl/20889656?noredirect=1#comment75064909_20889656), the code example that you give doesn't show the use of `local`. However, you then explain the scope of `local`, but it hasn't been demonstrated. The problem is that you've copied the wrong code snippet from the perlmonks page. It's rather misleading. – Greenonline Jan 14 '21 at 09:35
5

Private Variables via my() is the primary documentation for my.

In the array size example you mention, it's not used to find the size of the array. It's used to create a new variable to hold the size of the array.

cjm
  • 59,511
  • 9
  • 121
  • 166