Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: Evaluate variable while using

by BillKSmith (Monsignor)
on Oct 21, 2022 at 15:55 UTC ( [id://11147575] : note . print w/replies, xml ) Need Help??

in reply to Evaluate variable while using

If you have several variables which require this behavior, it is convenient to write a function (str_maker) which generates the closures for you.
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.