Missing first arg in list causes error

Paul Johnson paul at pjcj.net
Sat Feb 11 15:51:32 GMT 2006


On Sat, Feb 11, 2006 at 07:54:27AM -0600, Andy Lester wrote:

> >  $ perl -le 'print [,"",,]'
> >
> >Why does the first one fail?
> 
> Because it's not valid syntax.  You can have an empty trailing comma,  
> but not a leading one.

But why not?  The docs explain:

  The null list is represented by ().  Interpolating it in a list has no
  effect.  Thus ((),(),()) is equivalent to ().  Similarly,
  interpolating an array with no elements is the same as if no array had
  been interpolated at that point.

  This interpolation combines with the facts that the opening and
  closing parentheses are optional (except when necessary for
  precedence) and lists may end with an optional comma to mean that
  multiple commas within lists are legal syntax. The list "1,,3" is a
  concatenation of two lists, "1," and 3, the first of which ends with
  that optional comma.  "1,,3" is "(1,),(3)" is "1,3" (And similarly for
  "1,,,3" is "(1,),(,),3" is "1,3" and so on.)  Not that we’d advise you
  to use this obfuscation.

But this seems to be making a feature out of an implementation detail.
And if "1,,,3" is "(1,),(,),3", why isn't ",,3" "(,),3"?  And how did
"1,,,3" get to be "(1,),(,),3" and not "(1,),(),3" or "((1,),),3"?  In
fact, the whole thing seems rather dodgy, since what's really happening
is:

  argexpr	:	argexpr ','

Anyway, with the following patch, I get see:

  $ ./perl -le 'print [,"",,]'
  ARRAY(0x81bb1c8)

and all tests pass.  This is obviously not sufficient - it doesn't deal
with [,,3] for example, nor does it deal with (,3) - but it does show
that there is probably a reasonable solution out there, if we want one.

--- perly.y.org	2005-12-19 17:28:40.000000000 +0100
+++ perly.y	2006-02-11 16:29:14.217485923 +0100
@@ -581,6 +581,8 @@
 /* Constructors for anonymous data */
 anonymous:	'[' expr ']'
 			{ $$ = newANONLIST($2); }
+	|	'[' ',' expr ']'
+			{ $$ = newANONLIST($3); }
 	|	'[' ']'
 			{ $$ = newANONLIST(Nullop); }
 	|	HASHBRACK expr ';' '}'	%prec '(' /* { foo => "Bar" } */

-- 
Paul Johnson - paul at pjcj.net
http://www.pjcj.net


More information about the london.pm mailing list