Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I use the "my" keyword in Perl?

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?

like image 789
FistOfFury Avatar asked Jan 02 '14 18:01

FistOfFury


People also ask

What is $_ called in Perl?

There is a strange scalar variable called $_ in Perl, which is the default variable, or in other words the topic. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used. In general, I'd say you should NOT see $_ in real code.

What is the difference between my and local in Perl?

The short answer is that my marks a variable as private in a lexical scope, and local marks a variable as private in a dynamic scope.

Which keyword is used in Perl to define a function?

In Perl, the terms function, subroutine, and method are the same but in some programming languages, these are considered different. The word subroutines is used most in Perl programming because it is created using keyword sub.

What is @_ and $_ in Perl?

@ is used for an array. In a subroutine or when you call a function in Perl, you may pass the parameter list. In that case, @_ is can be used to pass the parameter list to the function: sub Average{ # Get total number of arguments passed. $ n = scalar(@_); $sum = 0; foreach $item (@_){ # foreach is like for loop...


2 Answers

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

like image 146
ikegami Avatar answered Sep 20 '22 14:09

ikegami


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

  • What is the difference between my and our in Perl?

(Thanks to ThisSuitIsBlackNot).

like image 40
Igor Chubin Avatar answered Sep 19 '22 14:09

Igor Chubin