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?
my is used for local variables, whereas our is used for global variables.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With