http://www.perlmonks.org?node_id=911148

tj_thompson has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks. I have a question I'm sure has a very easy answer, but I'm not quite sure how to look for it.

I have a variable with some value in it. I would like to create an anonymous subroutine that will return the value of the variable. However, I'd like to lock in the return value of the subroutine to be the value of the variable when I create the subroutine as opposed to the current value of the variable when the subroutine is called. For example:

DB<10> $str = 'hi'; DB<11> $sub = sub {$str} DB<12> x &$sub 0 'hi' DB<13> $str = 'hello'; DB<14> x &$sub 0 'hello'

I would like the second call to &$sub to return 'hi' also, instead of the new value of $str. How do I force the variable to be interpolated at the time of the subroutine creation? I tried double quoting the var but that didn't do the trick either. I tried searching a bit, but the terms to use for this aren't obvious and I'm not getting much for results.

I appreciate any input. I'm sure I'll be giving myself a good facepalming shortly after someone points out the obvious :)

Replies are listed 'Best First'.
Re: simple anonymous subroutine and variable interpolation timing question
by ikegami (Patriarch) on Jun 23, 2011 at 20:07 UTC

    There's no interpolation involved. The sub contains a lookup of a variable's content, so the variable needs to remain unchange if you want the output of the lookup to be constant.

    You can do this by copying the value of $str into into a variable that doesn't change, and using that variable instead.

    sub make_closure { my ($str) = @_; return sub { $str }; } my $str = 'hi'; my $sub = make_closure($str); say $sub->(); # 'hi' $str = 'hello'; say $sub->(); # 'hi'

    You could actually interpolate the current value into code, but producing code is trickier.

    my $str = 'hi'; my $sub = eval qq{ sub { "\Q$str\E" } } or die $@; say $sub->(); # 'hi' $str = 'hello'; say $sub->(); # 'hi'

      I was considering the closure with a local variable, but I was wondering if there was some sneaky way the experts did it. I don't like the looks of the eval method, so I think I'll stick with the first.

      Thanks as always Ikegami!

        You don't have to create a separate sub. Alternatives:

        my $sub = sub { my ($str) = @_; sub { $str } }->($str);
        my $sub = sub { my $str = $str; sub { $str } }->();
        my $sub = do { my $str = $str; sub { $str } };
        my $str_copy = $str; my $sub = sub { $str_copy };

        In practice, it's usually a non-issue since you either have a variable that doesn't change to begin with

        for my $str (...) { ... sub { ... $str ... } ... }
        or you want to factor out the code to a sub anyway.
        sub make_list_iterator { my @list = @_; return sub { return @list ? shift(@list) : (); }; } my $iter = make_list_iterator(@list);
        errr, meant lexical variable, not local.
Re: simple anonymous subroutine and variable interpolation timing question
by wind (Priest) on Jun 23, 2011 at 21:20 UTC
    I see three possible solutions to this:
    1. 1) Save the value in another variable, using Readonly if you want to ensure the saved value doesn't get modified.
    2. 2) Use a closure.
    3. 3) Interpolate the variable in an eval'd anonymous subroutine.
    use Readonly; my $str = 'hi'; # String Readonly my $STR => $str; # Closure my $closure = do { my $var = $str; sub { $var } }; # Evalled Sub my $anonsub = do { (my $var = $str) =~ s/(["\\])/\\$1/g; eval qq{sub {"$var"}}; }; $str = 'hello'; print "string = $STR\n"; print "closure = ", $closure->(), "\n"; print "anonsub = ", $anonsub->(), "\n";
Re: simple anonymous subroutine and variable interpolation timing question
by Anonymous Monk on Jun 24, 2011 at 10:50 UTC
    state
    $ perl -e " use feature 'state'; sub Foo { state $block = $_[0]; $bloc +k } warn Foo(6); warn Foo(29000); " 6 at -e line 1. 6 at -e line 1.