[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.
Excellent book. Do more than just "breeze" through it. Study it thoroughly, it's probably the single best starting place to learn perl. > 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. The first line is better written as opendir(DIR, $some_dir) or die "can't opendir $some_dir: $!"; Note the use of 'or' instead of '||'. Precedence is extremely important, especially in perl where so much of the code can be "short-circuited". > 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 ? One minor problem, there is a comma missing before the 'readdir': @dots = grep { /^\./ && -f "$some_dir/$_" }, readdir(DIR); > In C or Java you would write > grep(readdir(DIR)) most likely ? No, you would use a while or for loop and then search for files not beginning with a dot. Sure, you can embed functions in perl (duh), but you have to put it into a loop construct. Here, in perl, it is implied. > If that is so, what is all that business with the curly braces ? > I thought curly braces are supposed to denote code sections ? You mean scope definition? Someone else can do a better job than I can on this, but you can use them basically whenever you want, or to combine sections of code/statements. > So, it's like the output of readdir is "trickling" through the curly > braces and then the output after this is "trickling" through grep ? It's a matter of precedence, really. I wouldn't say it is "trickling", rather one function completing and passing it's result to the next one, just as you can in C/C++. > If so, what is the "input" to the code section in the curly braces ? It's readdir(). So while-readdir, search for files (only) and test if they DONT begin with a dot, then store them into an array. Now that we have the files that match what we want, we close the directory since we don't need it any longer. > Anyhow, all this is a bit weird to me, so I was hoping someone could > shed some light on it. Perl can be written more 'C like', but why not take advantage of it's combination power whenever practical (and readable!). > thanks and cheers, Hope this helps.... > p.s. It seems some of my confusion comes from the fact that I am used to > putting () around funtion calls. That still doesn't help with the curly > braces though ... %^) Parens are often optional. When in doubt, use them. I never use them in simple print statemtents, but some functions like 'split', it just makes more sense. Your mileage may vary :) -Jeff __________________________________ Do you Yahoo!? New Yahoo! Photos - easier uploading and sharing. http://photos.yahoo.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>