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 1110833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:27:11+00:00 2026-05-17T02:27:11+00:00

I think this might be best asked using an example: use strict; use warnings;

  • 0

I think this might be best asked using an example:

use strict;
use warnings;
use 5.010;
use Storable qw(nstore retrieve);

local $Storable::Deparse = 1;
local $Storable::Eval    = 1;

sub sub_generator {
    my ($x) = @_;

    return sub {
        my ($y) = @_;
        return $x + $y;
    };
}

my $sub = sub_generator(1000);
say $sub->(1); # gives 1001
nstore( $sub, "/tmp/sub.store" );
$sub = retrieve("/tmp/sub.store");
say $sub->(1); # gives 1

When I dump /tmp/sub.store I see:

$VAR1 = sub {
            package Storable;
            use warnings;
            use strict 'refs';
            my($y) = @_;
            return $x + $y;
        }

But $x is never defined in this sub. I would expect that the sub generated by sub_generator will have $x replaced with its actual value upon generation. How should I solve this?

Note this question relates to this one.

  • 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-05-17T02:27:12+00:00Added an answer on May 17, 2026 at 2:27 am

    Unfortunately I don’t think Storable works with closures. However there are other CPAN modules that will serialise a closure. For eg. Data::Dump::Streamer

    use 5.012;
    use warnings;
    use Data::Dump::Streamer;
    
    sub sub_generator {
        my ($x) = @_;
    
        return sub {
            my ($y) = @_;
            return $x + $y;
        };
    }
    
    my $sub = sub_generator(1000);
    say $sub->(1); # gives 1001
    
    my $serialised = Dump( $sub )->Out;
    my $copy = do {
        my $CODE1 = undef;
        eval $serialised;
        $CODE1;
    };
    
    say $copy->(2); # gives 1002
    say $sub->(1);  # still gives 1001
    

    This is what the serialised code looks like when printed here, say Dump $sub;:

    my ($x);
    $x = 1000;
    $CODE1 = sub {
               use warnings;
               use strict 'refs';
               BEGIN {
                 $^H{'feature_unicode'} = q(1);
                 $^H{'feature_say'} = q(1);
                 $^H{'feature_state'} = q(1);
                 $^H{'feature_switch'} = q(1);
               }
               my($y) = @_;
               return $x + $y;
             };
    

    Update

    See this thread Storable and Closures on the Perl5 porters mailing list. It confirms what I thought about Storable and closures.

    /I3az/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this might be a problem with the theme I'm using ( Nitobe
I mostly develop using C#, but I think this question might be suitable for
This might be something very straight forward and I really think it should work
Firstly, This might seem like a long question. I don't think it is... The
You might think that this question is already answered. However, I couldn't find the
I think my code might be written incorrectly and this is causing my problem.
I really nead to ask you this one, many of you might think it's
I think this might be a pretty simple question, but I haven't been able
I've asked a similar question to this here: Best way to interact with EJBs
This is a question on what people think the best way to lay out

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.