Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "my" a function in Perl?

Tags:

perl

I know that my is used to declare a variable local to a block or file. I have always assumed that my is a keyword in Perl. But I was just told that it's actually a function. One of the proofs is that perldoc puts my under the “Functions” section, see http://perldoc.perl.org/functions/my.html.

How does a function do the job of declaring local variables?

like image 598
Yu Hao Avatar asked Jun 28 '13 06:06

Yu Hao


People also ask

What is the difference between my and our in Perl?

my is used for local variables, whereas our is used for global variables.

What does @_ mean in Perl?

Using the Parameter Array (@_) Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order.

What does =~ do in Perl?

The operator =~ associates the string with the regex match and produces a true value if the regex matched, or false if the regex did not match.

What are the three categories of Perl variables?

Perl has three main variable types: scalars, arrays, and hashes. A scalar represents a single value: my $animal = "camel"; my $answer = 42; Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required.


1 Answers

my is not a function, it's just clumped together with functions (in perl documentation) because it works like a function.

If you look at perldoc perlfunc, it is saith,

Here are Perl's functions (including things that look like functions, like some keywords and named operators) arranged by category...

then a bit below that

Keywords related to scoping
caller, import, local, my, our, package, state, use

Specifically, note that the word “keyword” was used there instead of “function”

So that implies that you would find some non-functions (e.g. keywords) under Perl functions A-Z

Another way of saying this: if something is listed under “Functions” in perldoc, it is not necessarily a function – it can be a keyword or named operator which acts like a function.

like image 161
doubleDown Avatar answered Sep 19 '22 08:09

doubleDown