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: perl bug with $1, affected by subroutine call Message-ID: <7298@jpl-devvax.JPL.NASA.GOV> Date: 6 Mar 90 22:43:13 GMT References: <7291@jpl-devvax.JPL.NASA.GOV> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 62 In article jbw@bucsf.bu.edu (Joe Wells) writes: : Huh? Why is "&sub1" in "&one(&sub1)" being evaluated in an array context? : I intended it to be the single scalar argument to "&one". You can intend all you want--the single item passed to a subroutine call is a LIST, which can have any number of arguments. If you want a version of Perl that treats 1 argument differently from 0 arguments or 2 arguments, you can go back to Perl version 1.0, which made the distinction. It was a headache. Perl now believes that lists can have 1 element. : Since the "if" is the last statement of the sub block, it has to be : evaluated in the context that the subroutine was called. : : Huh? Does this mean that an "if" statement has a value, unlike in C? If : so, is there a precise specification of the value of an "if" statement? I : can't find that in the manual. If you look up subroutines you find: do SUBROUTINE (LIST) Executes a SUBROUTINE declared by a sub declaration, and returns the value of the last expression evaluated in SUBROUTINE. What was the last expression evaluated in your subroutine? : If an "if (EXPR) BLOCK" statement is evaluated in a certain context, does : that mean that EXPR is always evaluated in the same context? It seems : that EXPR should always be evaluated in a scalar context, and the value of : the "if" statement should be the value of BLOCK (evaluated in the : appropriate context). That's precisely what I was suggesting as a possible remedy in my article. : So the /(x)/ has it's array context behavior. : : Question about this. Consider this statement: : : @a = /xxxxxxx/; : : What is the value of @a if the match succeeds? (), right? : : What is the value of @a if the match fails? (), right? At this point the documentation lies slightly. It implies that you'd get a null array either way. In fact, it only pays attention to the array context if there are any parens or if it fails, so you'd get an array with a value of (1) if it matches, and () if it doesn't. Kind weird. I will fix the man page. Thanks. chdir '/usr/spool/news/comp/lang/perl' || die "Not a \"normal\" news server.\n"; sub bynum {$a - $b;} $/ = '\0'; $* = 1; $| = 1; opendir(DIR,'.'); @arts = sort bynum readdir(DIR); srand(time); print "Not "; while ($art = $arts[rand @arts]) { open art; $_ = ; next unless s/\n-- *\n.*Randal [^\0]*/\n/; s/[^\0]*\n\n//; next unless $_; eval $_; last unless $@; } Larry