use strict; use warnings; my %colors = ( blue => "#0000ff", red => "#ff0000", green => "#00ff00", ); { no strict; while (my ($color, $color_code) = each(%colors)) { *$color = sub { "@_"; }; } } print red("Hello"), "\n"; print green("World"), "\n";