June 2005
Beginner
480 pages
10h 31m
English
Examine the following block of code:
sub bar {
($a,$b)=@_;
$b=100;
$a=$a+1;
}
sub foo {
my($a)=67;
local($b)=@_;
bar($a, $b);
}
foo(5,10)
| 1: | After you run bar($a, $b), what is the value in $b?
|
| 2: | What is the return value from foo()?
|
| 3: | Inside foo(), how is $b scoped?
|
| A1: | b. $b is declared with local in foo() so that every called subroutine shares the same value for $b (unless they later declare $b again with local or my). After calling bar(), where $b is modified, $b is set to 100. |
| A2: | b. Surprised? The last statement in foo() is bar($a, $b). bar() returns 68 because the value of $a is passed to bar(), and it's incremented. foo() returns the value of the last expression, which is ... |
Read now
Unlock full access