Luke Bakken wrote:
> 
> I'd thought I'd seen everything with perl until I saw John Krahn give
> this code as a solution:
> 
> #syntax:  unfold.pl filename > newfilename
> 
> @ARGV or die "usage: $0 filename\n";
> $/ = '';
> print for grep [ chomp, s/\n\s+/ /g, s/\z/\n\n/ ], <>;

It's just a different way of writing:

print for map { chomp; s/\n\s+/ /g; s/\z/\n\n/; $_ } <>;


Perhaps I shouldn't get too cute on the beginners list?  :-)


> I've never seen grep used with [ ] brackets before - it works the same
> with { }:
> 
> print for grep { chomp, s/\n\s+/ /g, s/\z/\n\n/ } <>;

This is not the same and does not do the same thing.

> How do the brackets fit into the "grep EXPR, LIST" form given in the
> docs?

grep has two forms:

grep { BLOCK } LIST

And:

grep EXRESSION, LIST


The first form allows multiple statements and lexically scoped variables
because it uses a code block.  The second form must have a single
expression.  grep evaluates the expression and if it is true passes the
current list element through to the left.  Putting [] around the
expression creates an anonymous array which in boolean context is always
true so that all elements of the list are passed through with the side
effect that they are modified by the expression in the anonymous array.


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to