Question: sub foo { my $lex = $ _ [ 0 ] ; sub bar { print $lex ; } bar ( ) ;

sub foo {
my $lex = $_[0];
sub bar {
print "$lex
";
}
bar();
}
foo(2); foo(3);
a) What is the output? What is (potentially) surprising about the output?
b) Modify the code to create a reference to an anonymnous local subroutine and verify that it creates closures correctly. give 2 different examples
c) Add the line "use diagnostics;" to the beginning of the original version of the program, and rerun the program. Based on the explanation this will give you, speculate as to how nested named subroutines are implemented in Perl 5.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!