Path: utzoo!mnetor!tmsoft!torsqnt!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!ames!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Are only simple scalars allowed in "do SUBROUTINE (LIST)" ? Message-ID: <7269@jpl-devvax.JPL.NASA.GOV> Date: 5 Mar 90 18:03:12 GMT References: <1211@frankland-river.aaii.oz.au> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 50 In article <1211@frankland-river.aaii.oz.au> pem@frankland-river.aaii.oz.au (Paul E. Maisano) writes: : The manual section for "do SUBROUTINE (LIST)" says: : SUBROUTINE may be a scalar variable, ... The parentheses are : required to avoid confusion with the "do EXPR" form. : : I always assumed that an array reference could be considered a scalar. A scalar value, but not a scalar variable. : In particular I thought the following was allowed. : : #!/usr/bin/perl : sub sub1 {print "1\n";} : sub sub2 {print "2\n";} : @subs = ('sub1', 'sub2'); : do $subs[1](); : : Unfortunately it gives a syntax error. : : I think this is a pity. It is a useful thing to be able to do. : Allowing an arbitrary EXPR instead of a scalar would be even better :-) I might be able to get away with it in this part of the grammar, but there are other similar parts of the grammar that I couldn't, and I have to watch out for non-orthogonalities... On top of which, there's the readability issue. I also have people arguing for ('foo','bar')[$which]. Maybe. I'm not done thinking about it. It's certainly more powerful to have [] and () as first class operators, but I'm not sure that such conciseness is always a virtue. : Of course, we could always just use eval instead. : Not as elegant though (or as efficient, maybe). More efficient than eval would be to use a temp variable: @subs = ('sub1', 'sub2','sub3','sub4'); $whichsub = $subs[index($blather{$token},pack("c",$current_state))]; do $whichsub(*my_stack); I almost like that better than do $subs[index($blather{$token},pack("c",$current_state))](*my_stack); There's something to be said for naming your intermediate products. Larry