in reply to Evaluate variable while using
use strict; use warnings; use feature 'say'; sub str_maker{ my $fixed = $_[0]; my $dynamic = \$_[1]; my $closure = sub{ return $fixed . ', ' . $$dynamic; }; return $closure; } my ($aa, $bb); my $static; $static = 'Hello'; my $str1 = str_maker($static, $aa); $static = 'Greetings'; my $str2 = str_maker($static, $bb); $aa = 'Kevin'; say &$str1; # Hello, Kevin $bb = 'World'; say &$str2; # Greetings World $aa = 'Bill'; say &$str1; # Hello, Bill $bb = 'Aliens'; say &$str2; # Greetings, Aliens
Minor point: $a and $b are poor choices for variable names because they may clash with variables of the same name used in sort. I used $aa and $bb instead.
|
|---|