by mfontani · 4y ago · 0 comments
Same as why there's "var", "let" or "const" in JS. Different scoping / types of variable slots for the interpreter.
A "my" variable declares a variable valid in the current scope; an "our" variable will be available at the package level; a "state" variable is initialised only once, but has the same scope as a "my" variable; a "local" variable "locally overrides" for the current block (and any code called by the current block) the value of said variable.
They all have their uses, and that's why they exist.
(Although I agree that a javascript programmer might get confused by declaring an object or an array as a const and then being allowed to mutate it.)
For instance:
my @arr = ("Larry", "Moe", "Curly");
my $len = @arr;
will store 3 in $len. Application codebases and libraries make use of this kind of thing everywhere.
You can find some of the reasoning why in interesting essays like this: http://www.wall.org/~larry/natural.html
Larry Wall was trying to incorporate some natural language features in Perl, unsuccessfully in this case but I would say it went very well in others.