in reply to Getting a Hash Name

A hash reference doesn't have a name. References in general don't have names. Sure, you can take a reference to a named object, like you are doing, but you can also take a reference to an anonymous object, like this:

check_limit( +{ one => 1, two => 2 });

This has exactly the same semantics, as far as the subroutine is concerned, as the way you are doing it. Either way, you are passing a reference to a hash, so the subroutine gets a reference that points to a hash data structure. But it does not get a name, because references are anonymous. If you want to pass the *name* of the hash to the subroutine, you'll have to accomplish that another way, e.g., by passing the name as a string, or by passing a glob, or whatever.

The reason I don't pass the whole hash is because these hash-puppies can get quite large and I'd like to avoid copying them.

Passing a copy of the hash doesn't pass the name either; it just passes (a copy of) the contents. Your subroutine gets to set up its own name for the contents, as in:

sub check_limit { my %happyhash = @_; # It is now named %happyhash within the subroutine. my $num = keys %happyhash; print "Hash: happyhash has $num keys\n"; }

You can also do this if the hash is passed by reference, but the syntax is slightly different:

sub check_limit { my %happyhash = %{$_[0]}; # It is now named %happyhash within the subroutine. my $num = keys %happyhash; print "Hash: happyhash has $num keys\n"; }

So now you have your own name, within the subroutine, for the thing. The calling code that passed it to you may have had a different name, or the same name, or no name at all, or more than one name, for the same thing, but you don't know that, because that information is not passed. If you need that information, you have to get it another way, such as by having the calling code pass that in separately, as someone else suggested.

Also note that if the hash is large, you could significantly improve the efficiency of your subroutine by calling the builtin keys in numeric context to get the number of keys, rather than calling it in list context and looping over the list to count up to the same number. I'm not sure if that matters, since I'm not sure if your subroutine is real or a made-up example to illustrate your other question, but I thought I'd mention it.


"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68