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

In reply to Re: Getting a Hash Name by jonadab
in thread Getting a Hash Name by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.