jefferson
jefferson

Reputation: 23

What does "my (%input)" mean in Perl?

I just started on a new job, which includes maintaining some Perl code. Unfortunately, I am a newbie in Perl but I did my best to search for all answers through books and online, and couldn't find any. My question is straightforward actually - what does the following lines means?

my(%input);
my(%query);

I understand that "my (List of variables)" from different sources is to declare a list of variables, as well as the % sign being a hash, but does not find any reference for my (%xxx).

I went through the CGI script that contains this but did not find any reference to the above two lines, i.e. as %input or $input. No module etc is also being specified. Can I safely delete these two lines?

I really ran out of ideas so will appreciate any advice.

Upvotes: 2

Views: 366

Answers (2)

Dave Cross
Dave Cross

Reputation: 69314

The perldoc web site is the best place to get information about Perl. Random Googling for Perl information can lead you to some right dodgy places.

The my function is defined in the perlfunc manual page. Full details are here.

Details of how the various types of Perl variables are declared and used can be found in the perldata manual page.

Note that your hash %input could be referred to as %input, $input{key} or @input{list of keys}.

Can I safely delete these two lines?

What happens if you try that? Or, rather than deleting them, perhaps just comment them out at first. The Perl comment character is #.

Update: To attempt to explain what I think is the real source of confusion here.

As it says in the documentation, my takes a list of variables. So you see things like this:

my ($foo, @bar, %baz);

Which declares three variables, a scalar, an array and a hash.

Of course, a single item list is still a list. So you can declare a single variable.

my ($foo);

In this case, the parentheses are optional. You get the same effect from

my $foo;

But if you're declaring more than one variable, the parentheses are mandatory;

my $foo, @bar, %baz; # This doesn't work

So, in the original question we have:

my(%input);
my(%query);

If it was my code, I'd write that as

my (%input, %query);

But either of them are exactly the same as

my %input;
my %query ;

It doesn't matter which variation you choose.

It should also be noted that the call to my is often used on the left hand side of an assignment operator, which will then initialise the variables with the values from the right hand side of the operator.

my ($scalar, $another_scalar, $a_third_scalar) = (1, 10, 100);

Here, both sets of parentheses are necessary. In both cases they are used to construct lists.

To explicitly answer another question from a comment here:

What is my (%input) declaring?

It is declaring a hash called %input. The hash is initially empty.

Upvotes: 3

DeVadder
DeVadder

Reputation: 1404

Does the script have use strict; use warnings;? Anyways, you can initialize more than one variable with parens. For example, you could have my ($in, $out); instead of my $in; my $out;. This is often done to group variables by something. In this case you could omit the parens. Maybe the author grouped some other variables and wanted to indicate here that those two hashes are not similar to any other group of variables?

If your two variables never show up again, feel free to delete the lines. And if you use strict; (what you really, really should), you will be notified in case they actually do get used later.

P.s. Your hashes could also show up with array sigils as hash slices, for example like @input{$key1, $key2}. See http://perldoc.perl.org/perldata.html#Slices

Upvotes: 2

Related Questions