Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: simple anonymous subroutine and variable interpolation timing question

by ikegami (Patriarch)
on Jun 23, 2011 at 20:07 UTC ( #911150=note: print w/replies, xml ) Need Help??

in reply to simple anonymous subroutine and variable interpolation timing question

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'

Replies are listed 'Best First'.
Re^2: simple anonymous subroutine and variable interpolation timing question
by tj_thompson (Monk) on Jun 23, 2011 at 21:09 UTC

    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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://911150]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2023-03-30 08:35 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (74 votes). Check out past polls.