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

  • SEARCH
  • Home
  • 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 8550047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:51:42+00:00 2026-06-11T13:51:42+00:00

As the title says, in Perl, how can I save a hash that contains

  • 0

As the title says, in Perl, how can I save a hash that contains a list of subroutine references? For example, I have the following hash that contains the references to the subroutines which are contained in other libraries:

my %testMap = (
  helloTest        => \&runHello,
  goodbyeTest      => \&runGoodbye,
);

When I try to use Data::Dumper in the following matter:

my($out) = new FileHandle ">$fileName"; 
my $serialized => Data::Dumper->Dump([\%testMap], [$HASH_REFERENCE]); 
print $out $serialized; 
close $out;

I end up with a file that looks like the following:

$testMap = {
             'goodbyeTest' => sub { "DUMMY" },
             'helloTest' => sub { "DUMMY" }
           };

When I would like the output to look like what appears in the original listing, is there a way to do this?

Some experimentation with Data::Dumper and Storable have so far turned up nothing and I suspect that it is due to the actual code for the references not being available to the code that is running.

  • 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-11T13:51:43+00:00Added an answer on June 11, 2026 at 1:51 pm

    Storable has been able to serialize coderefs since version 2.05.

    use strict;
    use warnings;
    use Storable;
    use Data::Dump 'dump';
    
    {
        no warnings;             # Suppress 'used only once' warning
        $Storable::Deparse = 1;  # Needs to be set to true as per docs
        $Storable::Eval    = 1;       # Same as above
    }
    
    sub hello_world { print "Hello world!\n" }
    
    my %hash = (
                 helloTest => \&hello_world,
                 byeTest   => sub { print "Goodbye!\n" },
               );
    
    store \%hash, 'file';            # Could use freeze/thaw for
    my $cloned = retrieve( 'file' ); # in-memory serialization
    
    $cloned->{helloTest}();          # Prints 'Hello world!'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

as the title says, is there a way I can have a clock that
As title says, I have a list of words, Like stopWords = [the, and,
As title says, i know that new throws an exception which can be caught,
Like the title says, how do I redirect every url that starts with http://example.com/shop
Well as the title says i have 2 web projects that have the same
Title says most of it. I have a div container that holds all the
Title says all. I'll have navigation that looks like this and it's dynamically created
As title says. I want to perform and order by on a list -
As title says, I have access to the current listitem - and it's easy
The title says it mostly. If I have an object in Python and want

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.