I have been trying examples for hours but I can\'t seem to grasp how to do what I want to do.
I want to return a hash from a subroutine, and I figured a reference wa
First off, as mpapec mentioned in comments, use strict; use warnings;. That will catch most common mistakes, including flagging most of the problems you're asking about here (and usually providing hints about what you should do instead).
Now to answer questions 1 and 2:
%hash is the hash as a whole. The complete data structure.
$hash{key} is a single element within the hash.
Therefore, \%hash is a reference to %hash, i.e., the whole hash, which appears to be what you intend to return in this case. \$hash{key} is a reference to a single element.
Where it gets tricky in your second question is that references are always scalars, regardless of what they refer to.
$hash_ref = \%hash
To get an element out of a hash that you have a reference to, you need to dereference it first. This is usually done with the -> operator, like so:
$hash_ref->{key}
Note that you use -> when you start from a reference ($hash_ref->{key}), but not when you start from an actual hash ($hash{key}).
(As a side note on question 2, don't prefix sub calls with & - just use authUser() instead of &authUser(). The & is no longer needed in Perl 5+ and has side-effects that you usually don't want, so you shouldn't get in the habit of using it where it's not needed.)
For question 3, if you're only going to check once, you may as well just loop over the array and check each element:
my $valid;
for my $username (@list_of_users) {
if ($login eq $username) {
$valid = 1;
last; # end the loop since we found what we're looking for
}
}
if ($valid) {
print "Found valid username $login\n";
} else {
print "Invalid user! $login does not exist!\n";
}