Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:03:20+00:00 2026-06-04T17:03:20+00:00

In Perl, I’m learning how to dereference ‘subroutine references’. But I can’t seem to

  • 0

In Perl, I’m learning how to dereference ‘subroutine references’. But I can’t seem to use a subroutine reference as a hash ‘key’.

In the following sample code,

  1. I can create a reference to a subroutine ($subref) and then dereference it to run the subroutine (&$subref)
  2. I can use the reference as a hash ‘value’ and then easily dereference that
  3. But I cannot figure out how to use the reference as a hash ‘key’. When I pull the key out of the hash, Perl interprets the key as a string value (not a reference) – which I now understand (thanks to this site!). So I’ve tried Hash::MultiKey, but that seems to turn it into an array reference. I want to treat it as a subroutine/code reference, assuming this is somehow possible?

Any other ideas?

use strict;
#use diagnostics;
use Hash::MultiKey;    

my $subref = \&hello;

#1: 
&$subref('bob','sue');               #okay

#2:
my %hash;
$hash{'sayhi'}=$subref;
&{$hash{'sayhi'}}('bob','sue');      #okay

#3: 
my %hash2;
tie %hash2, 'Hash::MultiKey';
$hash2{$subref}=1;
foreach my $key (keys %hash2) {
  print "Ref type is: ". ref($key)."\n";
  &{$key}('bob','sue');              # Not okay 
}

sub hello {
    my $name=shift;
    my $name2=shift;
    print "hello $name and $name2\n";
}

This is what is returned:

hello bob and sue
hello bob and sue
Ref type is: ARRAY
Not a CODE reference at d:\temp\test.pl line 21.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:03:22+00:00Added an answer on June 4, 2026 at 5:03 pm

    That is correct, a normal hash key is only a string. Things that are not strings get coerced to their string representation.

    my $coderef = sub { my ($name, $name2) = @_; say "hello $name and $name2"; };
    my %hash2 = ( $coderef => 1, );
    print keys %hash2;  # 'CODE(0x8d2280)'
    

    Tieing is the usual means to modify that behaviour, but Hash::MultiKey does not help you, it has a different purpose: as the name says, you may have multiple keys, but again only simple strings:

    use Hash::MultiKey qw();
    tie my %hash2, 'Hash::MultiKey';
    $hash2{ [$coderef] } = 1;
    foreach my $key (keys %hash2) {
        say 'Ref of the key is: ' . ref($key);
        say 'Ref of the list elements produced by array-dereferencing the key are:';
        say ref($_) for @{ $key }; # no output, i.e. simple strings
        say 'List elements produced by array-dereferencing the key are:';
        say $_ for @{ $key }; # 'CODE(0x8d27f0)'
    }
    

    Instead, use Tie::RefHash. (Code critique: prefer this syntax with the -> arrow for dereferencing a coderef.)

    use 5.010;
    use strict;
    use warnings FATAL => 'all';
    use Tie::RefHash qw();
    
    my $coderef = sub {
        my ($name, $name2) = @_;
        say "hello $name and $name2";
    };
    
    $coderef->(qw(bob sue));
    
    my %hash = (sayhi => $coderef);
    $hash{sayhi}->(qw(bob sue));
    
    tie my %hash2, 'Tie::RefHash';
    %hash2 = ($coderef => 1);
    foreach my $key (keys %hash2) {
        say 'Ref of the key is: ' . ref($key);   # 'CODE'
        $key->(qw(bob sue));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perl's system() starts a process, but breaks the parent/child relationship? test.pl: use POSIX; system(./test.sh
Perl newbie here. Can someone kindly show me how to use perl to extract
In perl debugger I can use DB::get_fork_TTY() to debug both parent and child process
In Perl 5.10, how do I create and access a hash with scalar keys
In perl I can write ($var1, $var2, $var3) = split(/:/, foo:bar:blarg) to split a
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Perl noob here - i have the following script if(substr($pc, 3,1)!= ){ $newpc =
Perl newbie here...I had help with this working perl script with some HASH code
Perl is complaining that it can't find a module that is right there in
In Perl, I can do: my @unit_indices = sort { $units{$b}[0] <=> $units{$a}[0] or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.