[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Modifiers



Thanks for the answers. Even if from my point of view (and I guess this is a personnal opinion) the more legible code I saw is from Ronald, I think that for this one I will stick with the "the clean, canonical way " (cf Chris).

Cheers

-Emmanuel
  

Ė (At) 12:31 -0500 05/02/99, Ronald J. Kimball ˇcrivait (wrote) :
>Emmanuel. M. Decarie wrote:
>> 
>> What I find ultra cool in Perl are modifiers, and I try to use them often. But I can't find for this expression the appropriate syntax and I was wondering if its possible to pack this in one line (and stay legible). 
>> 
>> @list =  (1..9);
>> 
>> foreach(@list) {print "$_\n"} if (@list);  # Syntax error...
>> 
>> or
>> 
>> (@list) ? foreach(@list) {print "$_\n"} : print "Empty list\n"; #Syntax error...
>> 
>
>The problem is that you cannot use a foreach loop as a simple statement or
>as an expression.
>
>You can, however, use a do {} block as a simple statement or an expression:
>
>  do { foreach(@list) {print "$_\n"} } if @list;
>
>  @list ? do { foreach(@list) {print "$_\n"} : print "Empty list\n";
>
>
>Another way to do it would be:
>
>  {
>    local ($,, $\) = ("\n", "\n");
>    print +(@list ? @list : "Empty list");
>  }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Emmanuel M. Dˇcarie - <emm@cam.org>
---> The Frontier Newbie Toolbox:
<http://www.cam.org/~emm/frontierNewbieToolbox.html>
---> Frontier en fran¨ais ! 
<http://www.scripting.com/frontier5/francais/>
<http://www.scriptmeridian.org/tutorials/francais/odb/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch