Re: please explain the anonymous sub usage in one of the article from stonehenge in detail for me

2008-11-07 Thread Richard Lee

Jenda Krynicky wrote:

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

sub read_from_stdin {
 return ;
}

...

$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.)


  
Excellent!!! this is very clear... (I mean I am sure article was very 
clear to regular readers.. i am just bit slow but this is great!!)


   I am not fully understanding the difference in anonymous sub vs just 
having regular  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.
  

thank you for the confirmation!!


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/




Re: please explain the anonymous sub usage in one of the article from stonehenge in detail for me

2008-11-07 Thread Jenda Krynicky
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() and 
> $next=$non_blank( sub{  })  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 ;
}

...

$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  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/




please explain the anonymous sub usage in one of the article from stonehenge in detail for me

2008-11-07 Thread Richard Lee

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() and 
$next=$non_blank( sub{  })  in terms of funcaitonality?
  I am not fully understanding the difference in anonymous sub vs just 
having regular  in there


2)in non_blank sub, my *guess is &{ $scanner }() is trying to 
dereference the anonymous sub routine... is () necessary there?


Please kindly explain as it's bit over my head at this point but I do 
enjoy the article immensenly..


thank you in advance.


$next = &non_blank(
   sub { ; }
   ); # read from stdin
   $next = &non_blank(
   sub { shift @cache; }
   }; # grab from list @cache



 sub non_blank {
   my($scanner) = @_;
   my($return);
   {
   $return = &{$scanner}();
   last unless defined $return;
   redo until $return =~ /\S/;
   }
   $return;
   }

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