Re: Syntax a little funny ....

2003-12-09 Thread John W. Krahn
[EMAIL PROTECTED] wrote:
> 
> On 12/08/03 19:33 or thereabouts, John W. Krahn scribbled:
> > [EMAIL PROTECTED] wrote:
> > > 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.
>
> I just read the function "prototype" for grep, and indead they have
> grep BLOCK LIST down as a possibility.
> This using a BLOCK where I usually expect something like "int x" or
> "String name" is sort of funny.
> %^)
>
> Is this fairly normal ? For a function to take a BLOCK as an
> "argument" like this ?

Well, it's not un-normal.  :-)  do, eval, grep, map, sort and sub can
all use a block as the first argument.

> I'm going to experiment with this, but I take
> it the BLOCK has to exit with a true/false value to let grep know
> whether to take that list item or not ?

That is correct.

> But other than that, I guess
> I could have print "i'm a grep block" inside there if i wanted ?

Yes, and the returned value from print would have determined whether to
pass the list element through or not.

> > Have you read the documentation for grep in perlfunc.pod?
>
> Ohh no ! Of course not. as usual I'm in too much of a hurry to learn
> everything all at once  there is only so much time to read
> man/info/perldoc pages after all.
> %^)
>
> One last thing.
> I noticed some people talking about the use of || vs. or.
> I'm sure this is in my Learning Perl book somewhere, but I would have
> guessed these were one and the same ... just alternate notations.

They do the same thing, however || has the same relative precedence as
its C language counterpart while Perl's 'not', 'and', 'or' and 'xor'
have lower precedence then all other operators.

perldoc perlop


John
-- 
use Perl;
program
fulfillment

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




Re: Syntax a little funny ....

2003-12-08 Thread John W. Krahn
[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]
 




Re: Syntax a little funny ....

2003-12-08 Thread R. Joseph Newton
[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);

@dots = grep ({ /^\./ && -f "$some_dir/$_" } readdir(DIR));

Same thing. What it does is to examine the lements of the list returned by
readdir, the second parameter of grep is the list being examined.  It uses
the boolean exprsession in the block--that the element being examined both
starts with a dot *and* is a directory.  The $somedir/ part of the
expression is necessary because the -x file tests use full paths.  Those
elements that pass both tests are passed into the list that is being
assigned to the array @dots.

Heres a sort of step by step:
foreach elemt of readdir dir
   next unless element beginns with '.'
   push into temp_list if element evaluates as a file
next
assign temp_list to @dots.

Joseph


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




Re: Syntax a little funny ....

2003-12-08 Thread Jeff Westman
James Edward Gray II <[EMAIL PROTECTED]> wrote:

> On Dec 8, 2003, at 11:28 AM, Jeff Westman wrote:
> 
> > [EMAIL PROTECTED] wrote:
> >
> >> 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".
> 
> Changing the || to or in the above line has no effect.  

Right, but too often one uses '||' and meant to use 'or'.  It bites me every
time .

>  However, as 
> Jeff said, parens in Perl are often optional on subroutine calls and if 
> we leave those out:
> 
> opendir DIR, $some_dir or die "can't opendir $some_dir: $!";
> 
> We get the traditional Perl open() call and here it matters if we use 
> || or or.  Why do we write it like that?  We like the way it reads.
> 
> >> 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);
> 
> The line compiles fine.  The block version of grep() does not require a 
> comma.

Right again  this is a case where using parens will get you :) .. I
usually do:

  @dots = grep /^\./ && -f "$some_dir/$_", readdir(DIR);

(no parens)

>  perldoc -f grep
> 
> > No, you would use a while or for loop and then search for files not 
> > beginning with a dot.
> 
> > 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
> 
> You say this twice, but that still doesn't make it true.  

Woops ... my bad!

> I think you 
> need to reread that grep() call.  ;)  We're searching for files that DO 
> begin with a dot here and placing them in the fitting @dots array.

My bad :)

> >> If that is so, what is all that business with the curly braces ?
> >> I thought curly braces are supposed to denote code sections ?
> 
> This is correct, it's a "chunk of code".  It will be executed once for 
> each item returned by readdir().
> 
> >> 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.
> 
> I think the easiest rule to start learning with in use parens for your 
> own subs and leave them off of built-ins, except when you need them to 
> clarify/enforce precedence.

I might add that parens can make your code more readable, but can also make
it harder to read some times too.  Parens often denote precedence, while
braces tend to be used for scoping.  This isn't a hard and fast rule though. 
Case-in-point, read:  perldoc -f map

-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]
 




Re: Syntax a little funny ....

2003-12-08 Thread James Edward Gray II
On Dec 8, 2003, at 11:28 AM, Jeff Westman wrote:

[EMAIL PROTECTED] wrote:

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".
Changing the || to or in the above line has no effect.  However, as 
Jeff said, parens in Perl are often optional on subroutine calls and if 
we leave those out:

opendir DIR, $some_dir or die "can't opendir $some_dir: $!";

We get the traditional Perl open() call and here it matters if we use 
|| or or.  Why do we write it like that?  We like the way it reads.

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);
The line compiles fine.  The block version of grep() does not require a 
comma.

perldoc -f grep

No, you would use a while or for loop and then search for files not 
beginning
with a dot.

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
You say this twice, but that still doesn't make it true.  I think you 
need to reread that grep() call.  ;)  We're searching for files that DO 
begin with a dot here and placing them in the fitting @dots array.

If that is so, what is all that business with the curly braces ?
I thought curly braces are supposed to denote code sections ?
This is correct, it's a "chunk of code".  It will be executed once for 
each item returned by readdir().

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.
I think the easiest rule to start learning with in use parens for your 
own subs and leave them off of built-ins, except when you need them to 
clarify/enforce precedence.

James

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



Re: Syntax a little funny ....

2003-12-08 Thread Jeff Westman
[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]
 




Re: Syntax a little funny ....

2003-12-08 Thread Robert Brown
That is a a great description of what is going on, but I think his
question centered on the &SYNTAX*, not the *SEMANTICS*.  As a
programmer from the "algebraic" school (FORTRAN, algol, pascal, C,
Ada, etc.), he is unacustomed to seeing a function call without
surrounding parens.  The key concept here, and one that is rather
"perly", is "list context" and "scalar context".

James Edward Gray II writes:
 > On Dec 8, 2003, at 10:52 AM, [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.
 > 
 > Okay, let's go step by step  readdir() is called (in list context), so 
 > it returns all entries in the directory.  Simple enough.
 > 
 > That returned list is immediately fed to another built-in, grep().  
 > grep() goes through every element in the list, checking it against some 
 > expression you provide (the part in the braces).  Inside the 
 > expression, the element you're currently checking is in Perl's default 
 > variable $_.  If the expression returns true for that element, it is 
 > included in grep()'s return list.  If not, it is omitted.
 > 
 > In this case, the expression check two things.  First, it uses a 
 > pattern match on the name to see if it begins with a dot (.).  If it 
 > does, it checks to make sure the entry is a file, to rule out entries 
 > like '.' and '..'.  If the entry passes both tests, grep() returns it.  
 > Otherwise, it's let out of the return list.  It may help to think of 
 > grep() as a filtering engine you can use to build on-the-fly lists.
 > 
 > Finally, the list returned by grep() is assigned to @dots, which is 
 > pretty much what it will contain, any file beginning with a dot (hidden 
 > files on a Unix system).
 > 
 > Here's a rough English translation of the line:
 > 
 > place in @dots all things beginning with . that are actually files from 
 > the listing for DIR
 > 
 > Hope that helps.
 > 
 > James
 > 
 > 
 > -- 
 > To unsubscribe, e-mail: [EMAIL PROTECTED]
 > For additional commands, e-mail: [EMAIL PROTECTED]
 >  
 > 
 > 
 > 

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




Re: Syntax a little funny ....

2003-12-08 Thread James Edward Gray II
On Dec 8, 2003, at 10:52 AM, [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.
Okay, let's go step by step  readdir() is called (in list context), so 
it returns all entries in the directory.  Simple enough.

That returned list is immediately fed to another built-in, grep().  
grep() goes through every element in the list, checking it against some 
expression you provide (the part in the braces).  Inside the 
expression, the element you're currently checking is in Perl's default 
variable $_.  If the expression returns true for that element, it is 
included in grep()'s return list.  If not, it is omitted.

In this case, the expression check two things.  First, it uses a 
pattern match on the name to see if it begins with a dot (.).  If it 
does, it checks to make sure the entry is a file, to rule out entries 
like '.' and '..'.  If the entry passes both tests, grep() returns it.  
Otherwise, it's let out of the return list.  It may help to think of 
grep() as a filtering engine you can use to build on-the-fly lists.

Finally, the list returned by grep() is assigned to @dots, which is 
pretty much what it will contain, any file beginning with a dot (hidden 
files on a Unix system).

Here's a rough English translation of the line:

place in @dots all things beginning with . that are actually files from 
the listing for DIR

Hope that helps.

James

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



Syntax a little funny ....

2003-12-08 Thread ebone+perl
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 ?

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

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

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

thanks and cheers,
e

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 ... %^)

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