102

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, it is 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?

2
  • 4
    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 2, 2014 at 20:03
  • If it's not really "yours", you should not use it.
    – icenac
    Jan 23, 2017 at 14:26

3 Answers 3

154

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

4
  • 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. Aug 10, 2020 at 7: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, 2020 at 7:52
  • 1
    I just meant that if people came to Perl from JS, it would be an easy way to explain the same concept Aug 10, 2020 at 8: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, 2020 at 8:52
31

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).

4
  • 3
    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. May 17, 2017 at 8:01
  • 2
    Why even mention local when you don't use it in the code example? Dec 16, 2020 at 15:48
  • @Greenonline: With this logic, if you have no code example at all, you shouldn't mention anything? Dec 16, 2020 at 21:31
  • 1
    Well yes. As mentioned in this comment, 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. Jan 14, 2021 at 9:35
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.