sub has_won - grep with adverbial block

 
 
 
sub has_won returns Bool {
    @letters == @solution.grep:{ $_ ne '' };
}

  • Methods with arguments require parens. However, the block to grep isn't really an argument. It's describing the manner in which the array will be grepped... that's an adverb to grep.
  • So, why are the parens required on methods? Take the following if statements:
    if @foo.shift { ... }
    
    if @foo.grep { ... }   # grep doesn't get the block
    	
    To make things clear, methods without parens are assumed to take no arguments. In order to pass a block to the above grep, you either need to use @foo.grep({ $^a <=> $^b}) or the adverbial colon:
    if @foo.grep:{$^a <=> $^b} { ... }
    	
  • Many thanks to Ashley Winters for his explanation of this