Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!bfmny0!tneff From: tneff@bfmny0.UU.NET (Tom Neff) Newsgroups: comp.lang.perl Subject: Re: Passing scalars by reference Message-ID: <15235@bfmny0.UU.NET> Date: 6 Mar 90 20:45:12 GMT References: <1220@frankland-river.aaii.oz.au> <7293@jpl-devvax.JPL.NASA.GOV> Reply-To: tneff@bfmny0.UU.NET (Tom Neff) Lines: 48 In article <7293@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes: > 3) Use the type glob *name to pass in the symbol name: ... > >This last method is the most powerful, because you can pass anything that has >a name in the symbol table, including filehandles, subroutines and formats. >What you're really doing is passing in the symbol table entry for everything >with that name and giving it a new name locally. Also note something not covered explicitly in the manual: This is the only way you can pass multiple arrays to a subroutine without effectively concatenating them into one amorphous array. For example if I want to be able to say @Questions = ("who", "what", "why"); @Answers = ("us", "this", "because"); @Weights = ("40", "35", "25"); $score = &process_one_quiz($pupil, @Questions, @Answers, @Weights); I cannot just say sub process_one_quiz { local ($p, @Q, @A, @W) = @_; ... } because @Q eats the rest of the @_ array including answers and weights. (The parameter list is ONE list.) But if I say sub process_one_quiz { local ($p, *Q, *A, *W) = @_; for $i ($[..$#Q) { print "$Q[$i],$A[$i],$W[$i]\n"; # (real work deleted) } } $score = &process_one_quiz($pupil, *Questions, *Answers, *Weights); then everything works right. Thus pass-by-reference can be an important tool for passing arrays even when the subroutine has no need to modify any elements. (Larry might want to mention this in the manual.) -- To have a horror of the bourgeois (\( Tom Neff is bourgeois. -- Jules Renard )\) tneff@bfmny0.UU.NET