[EMAIL PROTECTED] wrote:
> 
> I've just started trying to pick up a little perl.
> I'm breezing through "Learning Perl" and reading the perl docs.
> 
> Here is some syntax I found a little funny, and I was hoping somebody
> could explain this too me:
> 
> opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
> @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
> closedir DIR;
> 
> The first and 3rd lines I have no problem with.
> 
> Its that line with the grep in it.
> grep is obviously a built-in perl function.
> 
> This is something I've seen before ...
> the output of the function readdir is "trickling down" to become the
> input of the function grep ?
> 
> In C or Java you would write
> grep(readdir(DIR)) most likely ?
> 
> If that is so, what is all that business with the curly braces ?
> I thought curly braces are supposed to denote code sections ?

Yes, exactly, the curly braces allow the use of a code block instead of
a simple expression.  Having a code block means that you can have
multiple statements and lexical variables.


> So, it's like the output of readdir is "trickling" through the curly
> braces and then the output after this is "trickling" through grep ?

grep acts like a filter.  grep's expression determines whether an
element from the right hand list will be passed through to the left.


> If so, what is the "input" to the code section in the curly braces ?

grep aliases the variable $_ to the current element of the list on the
right hand side.


> Anyhow, all this is a bit weird to me, so I was hoping someone could
> shed some light on it.

Have you read the documentation for grep in perlfunc.pod?


> p.s. It seems some of my confusion comes from the fact that I am used to
> putting () around funtion calls.

perldoc perlfunc
[snip]
       Any function in the list below may be used either with or
       without parentheses around its arguments.  (The syntax
       descriptions omit the parentheses.)  If you use the paren­
       theses, the simple (but occasionally surprising) rule is
       this: It looks like a function, therefore it is a func­
       tion, and precedence doesn't matter.  Otherwise it's a
       list operator or unary operator, and precedence does mat­
       ter.  And whitespace between the function and left paren­
       thesis doesn't count--so you need to be careful sometimes:


> That still doesn't help with the curly braces though ... %^)

They can (IIRC) be used in four different ways: { code block }, $hash{
element }, ${'variable name'} and /match count{4,7}/.


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to