test.pl: ---------------------------------------------- sub say_hello { my $name = shift; my $text = 'Hello '.$name; my $sayAlert = sub { print "$text\n" }; $text = "Not in Perl you don't"; $sayAlert->(); } say_hello('Bob'); ---------------------------------------------- > perl test.pl Not in Perl you don't #### test.pl: ---------------------------------------------- sub say_hello { my $name = shift; my $sayAlert; { my $text = 'Hello '.$name; $sayAlert = sub { print "$text\n" }; } my $text = "Not in Perl you don't"; $sayAlert->(); } say_hello('Bob'); ---------------------------------------------- > perl test.pl Hello Bob #### function sayHello(name) { var sayAlert; { var text = 'Hello ' + name; sayAlert = function() { alert(text); } } var text='How confused am I?'; sayAlert(); } sayHello('Bob'); ---> "How confused am I?"