From: Richard Lee <[EMAIL PROTECTED]>
> http://www.stonehenge.com/merlyn/UnixReview/col08.html
> 
>  From above article, I am not fully understanding what's going on on 
> below code.
> 
> 1)is there any difference in $next = $non_blank(<STDIN>) and 
> $next=$non_blank( sub{ <STDIN> })  in terms of funcaitonality?

Sure. (except that there should have been &non_blank, not $non_blank. 
Or even better just non_blank()) In the first case the non_blank() 
subroutine obtains as its parameters all the lines that can be read 
from STDIN, while in the second case it obtains just one parameter. A 
reference to a subroutine that reads one line from STDIN whenever 
it's called.


If we name the subroutine the code would look like this:

sub read_from_stdin {
 return <STDIN>;
}

...

$next = non_blank(\&read_from_stdin);

that is again the non_blank() would receive a reference to a 
subroutine that reads one line from STDIN whenever called.
(actually this is not entirely true, the behaviour of the subroutine 
in both cases depends on the context. In scalar context it reads just 
one line, in list context it reads all remaining lines.)

>    I am not fully understanding the difference in anonymous sub vs just 
> having regular <STDIN> in there
> 
> 2)in non_blank sub, my *guess is &{ $scanner }() is trying to 
> dereference the anonymous sub routine... is () necessary there?

We need to dereference and thus call the subroutine, but you are 
right that the () is not necessary there. I like this syntax better 
though:

   $scanner->()

but it means the same.

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


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


Reply via email to