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

Re: [MacPerl] Modifiers



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");
  }


Ronald

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