Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Subroutines Returning Null Arrays Message-ID: <6990@jpl-devvax.JPL.NASA.GOV> Date: 5 Feb 90 23:31:00 GMT References: <13395@cit-vax.Caltech.Edu> <6712@ogicse.ogc.edu> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 71 In article <6712@ogicse.ogc.edu> hakanson@ogicse.UUCP (Marion Hakanson) writes: : In article <13395@cit-vax.Caltech.Edu> joe@cit-750.cs.caltech.edu (Joe Beckenbach) writes: : > : > Is there a way to force a subroutine to return a null array? : : Now I just tracked it down further. Here's a test program which : demonstrates the problem (perl 3.0, pl6, anyway -- I didn't notice : a patch in 7&8): : : ==========cut here=========== : #!/usr/bin/perl : : sub nsub1 : { : (); : } : : sub nsub2 : { : return (); : } : : : @nr1 = do nsub1(); : print "nr1 has $#nr1 elements.\n"; : : @nr2 = do nsub2(); : print "nr2 has $#nr2 elements.\n"; : ==========cut here=========== : : Here's the output: : : nr1 has -1 elements. : nr2 has 0 elements. : : It turns out that $nr2[0] is empty, which is why my "workaround" : works. Definitely looks like a bug to me, though. Larry? This turned out to be a syntactic problem. I had made return be a list operator so that you can say return 1,2,3; Unfortunately, like other list operators, if the next token was a left paren, that and the matching paren were taken to delimit the entire function. So return () transmogrified itself into (return); and there went the null array. This suggest several workarounds. You can say any of return (()); return ((),()); return +(); # !!! return @nullarray; return 1..0; The best solution, since return (123),456; makes no sense, is to exempt "return" from the parenthesis rule for other list operators, and have it ignore the parens. It then will do what is expected in almost every case. Larry