roger@gtisqr.uucp (Roger Droz) (02/15/91)
I prefer to code my subroutines with
local($foo, $bar) = @_
so that I can use more meaningful name than @_[2] in the subroutine
body. Since local converts scalars arguments to call by value, I
make extensive use of *name. One day, I managed to call a subroutine
that reused variable names as shown in the contrived example below.
Note the asymmetry in the treatment of scalars and arrays!
$foo = 'callers foo';
$bar = 'callers bar';
$fooey[0] = 'callers array';
$barey[0] = 'callers other array';
printf "%s: foo=%s, bar=%s\n", 'main', $foo, $bar;
printf "%s: fooey[0]=%s, barey[0]=%s\n", 'main', $fooey[0], $barey[0];
&mysub(*foo, *fooey);
printf "%s: foo=%s, bar=%s\n", 'main', $foo, $bar;
printf "%s: fooey[0]=%s, barey[0]=%s\n", 'main', $fooey[0], $barey[0];
sub mysub {
local(*bar, *barey) = @_;
local($foo) = 'subs foo';
local(@fooey) = ('subs array');
printf "%s: foo=%s, bar=%s\n", 'mysub', $foo, $bar;
printf "%s: fooey[0]=%s, barey[0]=%s\n", 'mysub', $fooey[0], $barey[0];
$bar = 'sub returns something by reference';
$barey[0] = 'sub modifies array';
}
main: foo=callers foo, bar=callers bar
main: fooey[0]=callers array, barey[0]=callers other array
mysub: foo=subs foo, bar=subs foo <-- why!?
mysub: fooey[0]=subs array, barey[0]=callers array <-- as expected.
main: foo=callers foo, bar=callers bar <-- value wasn't returned.
main: fooey[0]=sub modifies array, barey[0]=callers other array <-- expected.
____________
Roger Droz
() () Maverick MICRoSystems / Global Technology International
(_______) Mukilteo, WA
( )
| | Disclaimer: "We're all mavericks here:
| | Each of us has our own opinions,
(___) and the company has yet different ones!"
Internet: roger@mav.COM UUCP: uw-beaver!gtisqr!roger