Don Coyote has asked for the wisdom of the Perl Monks concerning the following question:
Writing a program to rename copied photos throughout several folders I am using several greps, at one stage I needed to filter a folder, rather than add another layer of grep I placed an if conditional inside an anonymous list constructor, and discovered that the ?: tertiary conditional may not actually be a synonym for if.
There is most likely a simple reason why the if statement(term/expression?) produces a syntax error if used inside a refto/list constructor, but I wondered if this was due to greater reason than being condusive to writing better code? (as in, hint: use if to control flow not determine variables)
from my examples here, I summise that the tertiary conditional is an if statement(term/expression), wrapped in a do block. Is this the case?
my @arr = ( if(1<2){'1'}else{()} ); #syntax error my $arrref = \( if(1<2){'1'}else{()} ); #syntax error my $arrref2 = [ 1<2 ? '1' : () ]; #works my @arr2 = ( do { if(1<2){'1'}else{()} } ); #works if(1<2){print '1',$/ }else{print '2',$/}; #works 1<2 ? print '1',$/ : print '2',$/; #works ( 1<2 ? print '1',$/ : print '2',$/ ); #works ( if(1<2){print '1',$/ }else{print '2',$/} ); #syntax error
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: conditional statements in list constructors
by AnomalousMonk (Archbishop) on Mar 27, 2014 at 14:05 UTC | |
Re: conditional statements in list constructors
by SuicideJunkie (Vicar) on Mar 27, 2014 at 14:59 UTC | |
Re: conditional statements in list constructors
by kcott (Archbishop) on Mar 27, 2014 at 14:48 UTC | |
by AnomalousMonk (Archbishop) on Mar 27, 2014 at 15:10 UTC | |
by kcott (Archbishop) on Mar 27, 2014 at 18:44 UTC | |
Re: conditional statements in list constructors
by Anonymous Monk on Mar 27, 2014 at 14:08 UTC | |
Re: conditional statements in list constructors
by Don Coyote (Hermit) on Mar 28, 2014 at 01:10 UTC |