Callbacks

Callbacks

am 09.09.2010 13:20:02 von Jatin Davey

--------------060000060301090208040408
Content-Type: text/plain;
charset="US-ASCII";
format="flowed"
Content-Disposition: inline

Hi

I am a newbie to Perl , I was reading through one of the beginner level
books on perl. I did not understand the concept of "Callbacks" and i
have the following questions on it:

1. What are they ?

2. Why do we need them ?

3. What useful purpose do they achieve ?

I was reading the following code to understand it but could not comprehend.

*Code:*

#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
find ( \&callback, "/");

sub callback {
print $File::Find::name, "\n";
}

*End Of Code:*

Thanks
Jatin

--------------060000060301090208040408--

Re: Callbacks

am 09.09.2010 13:39:42 von Alan Haggai Alavi

> Hi
>
> I am a newbie to Perl , I was reading through one of the beginner level
> books on perl. I did not understand the concept of "Callbacks" and i
> have the following questions on it:
>
> 1. What are they ?
>
> 2. Why do we need them ?
>
> 3. What useful purpose do they achieve ?
>
> I was reading the following code to understand it but could not comprehend.
>
> *Code:*
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use File::Find;
> find ( \&callback, "/");
>
> sub callback {
> print $File::Find::name, "\n";
> }
>
> *End Of Code:*
>
> Thanks
> Jatin


Hi Jatin,

A callback is a reference to a subroutine. This reference when passed around,
allows other code to invoke it.

File::Find's find() method accepts a subroutine reference as the first argument
and a path in the filesystem as the second argument. find() traverses
recursively in '/' and calls your code reference (callback()) for each
file/directory it finds. Thus, your subroutine is able to get each item in the
path as soon as they are encountered by File::Find's find(). If find() was not
implemented to handle callbacks, the possible way to return encountered
file/directory names will be as an array or hash of file/directory names after
it has traversed and exhausted all possible file/directory names within the
path.

Regards,
Alan Haggai Alavi.
--
The difference makes the difference.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 13:58:39 von chas.owens

On Thu, Sep 9, 2010 at 07:20, Jatin Davey wrote:
>  Hi
>
> I am a newbie to Perl , I was reading through one of the beginner level
> books on perl. I did not understand the concept of "Callbacks" and i have
> the following questions on it:
>
> 1. What are they ?
>
> 2. Why do we need them ?
>
> 3. What useful purpose do they achieve ?
>
> I was reading the following code to understand it but could not comprehen=
d.
>
> *Code:*
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use File::Find;
> find ( \&callback, "/");
>
> sub callback {
> print $File::Find::name, "\n";
> }
>
> *End Of Code:*
>
> Thanks
> Jatin
>

[Callbacks][0] are a method of generalizing code. A classic example
is sorting. A sorting algorithm is completely general except for one
thing: how to compare the data being sorted. So most sorting
algorithms are written to take a callback as well as the data to be
sorted. The callback knows how to compare two items that are being
sorted.

#!/usr/bin/perl

use strict;
use warnings;

sub insertion_sort {
my $compare =3D shift;
my @sorted;

OUTER:
for my $item (@_) {
for my $i (0 .. $#sorted) {
if ($compare->($sorted[$i], $item) == 1) {
#insert item before the sorted item
#that is larger than it
splice @sorted, $i, 0, $item;
next OUTER;
}
}
#this must be the biggest item so far, put it at the end
push @sorted, $item;
}

return @sorted;
}

sub sort_numerically { $_[0] <=3D> $_[1] }
sub sort_lexically { $_[0] cmp $_[1] }

my @unsorted =3D (4, 3, 2, 6, 5, 1, 10, 100, 20);

print join(", ", insertion_sort \&sort_numerically, @unsorted), "\n";
print join(", ", insertion_sort \&sort_lexically, @unsorted), "\n";

Now, creating sort_numerically is a pain, it would be much better if
we could just create the comparison function when we needed it.
Luckily Perl 5 has this capability: [anonymous subroutines][1] (you
might hear some people calling them lambdas). In the previous example
I could have just said

print join(", ", insertion_sort sub { $_[0] <=3D> $_[1] }, @unsorted), "\n"=
;
print join(", ", insertion_sort sub { $_[0] cmp $_[1] }, @unsorted), "\n";

One of the benefits of doing this is that it is now easy to reverse
the sort order:

print join(", ", insertion_sort sub { $_[1] <=3D> $_[0] }, @unsorted), "\n"=
;
print join(", ", insertion_sort sub { $_[1] cmp $_[0] }, @unsorted), "\n";


[0]: http://en.wikipedia.org/wiki/Callback_(computer_science)
[1]: http://en.wikipedia.org/wiki/Anonymous_function

--=20
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 16:25:45 von Jatin

> Hi Jatin,
>
> A callback is a reference to a subroutine.
[Jatin] if a callback is a reference to a subroutine then it can be
stored in scalar variable to call that subroutine. Am i correct in my
understanding ?
> This reference when passed around,
> allows other code to invoke it.
>
> File::Find's find() method accepts a subroutine reference as the first argument
> and a path in the filesystem as the second argument. find() traverses
> recursively in '/' and calls your code reference (callback()) for each
> file/directory it finds.
[Jatin] So in actual terms the File::Find's find() method in turn
invokes our callback subroutine but i am confused at one point , what is
"File::Find::name" ,

how does it return the complete path of the filename ?

If the File::Find's find method calls our subroutine , and we do not
have any lexical variable called as name then how come the execution of
our callback subroutine returns the complete path of the file name ?

Please clarify on these points.
> Thus, your subroutine is able to get each item in the
> path as soon as they are encountered by File::Find's find(). If find() was not
> implemented to handle callbacks, the possible way to return encountered
> file/directory names will be as an array or hash of file/directory names after
> it has traversed and exhausted all possible file/directory names within the
> path.
>
> Regards,
> Alan Haggai Alavi.
>

Thanks
Jatin

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 16:59:07 von chas.owens

On Thu, Sep 9, 2010 at 10:25, Jatin wrote:
>
>> Hi Jatin,
>>
>> A callback is a reference to a subroutine.
>
> [Jatin] if a callback is a reference to a subroutine then it can be stored
> in scalar variable to call that subroutine. Am i correct in my understanding
> ?

Yes, all types of references can be stored in scalars.

snip
> [Jatin] So in actual terms the File::Find's find() method in turn invokes
> our callback subroutine but i am confused at one point , what is
> "File::Find::name" ,
>
> how does it return the complete path of the filename ?
snip

$File::Find::name is a package variable from File::Find.
File::Find::find (i.e. the find function) sets that value each time
before it calls the callback.

snip
> If the File::Find's find method calls our subroutine , and we do not have
> any lexical variable called as name then how come the execution of our
> callback subroutine returns the complete path of the file name ?
snip

The callback is printing the value of $File::Find::name (which, again,
is set by the File::Find::find function before the callback is
called).


File::Find's interface is through its package variables. It is a very
bad design and is much regretted, but there is nothing to do about it
now.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 17:08:20 von Shawn H Corey

On 10-09-09 10:59 AM, Chas. Owens wrote:
> $File::Find::name is a package variable from File::Find.

$File::Find::name is a fully-qualified variable, just in case you're
wondering what "fully qualified" means.



--
Just my 0.00000002 million dollars worth,
Shawn

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 18:35:49 von Jatin

> File::Find's interface is through its package variables.
[Jatin] So my subroutine that i use with the File::Find's find method
has access to the package variables ? Correct me if i am wrong.
> It is a very
> bad design and is much regretted, but there is nothing to do about it
> now.
>
[Jatin] Totally agree , i was not able to co-relate this with the
fundamental idea of passsing references to other subroutines. But its
ok. Is there any plans to remedy this design in the future releases of
perl ?

Thanks Chas for clarifying on my points. Appreciate your help in this
regard.

Thanks
Jatin

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 18:38:11 von Jatin

On Thursday 09 September 2010 08:38 PM, Shawn H Corey wrote:
> On 10-09-09 10:59 AM, Chas. Owens wrote:
>> $File::Find::name is a package variable from File::Find.
>
> $File::Find::name is a fully-qualified variable, just in case you're
> wondering what "fully qualified" means.
>
>
>
[Jatin] No , i actually understood what a fully qualified variable is.

Thanks
Jatin

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 18:49:54 von Jatin

> The callback is printing the value of $File::Find::name (which, again,
> is set by the File::Find::find function before the callback is
> called).
>
>
>
[Jatin] Chas , when you say that the File::Find's find method sets its
name variable before the callback is called , Do you mean that this
"name" variable is set by the find function to the currently referenced
file or directory ? All the three variables namely $_ , $dir and $name
are set with values corresponding to the currently referenced file ?

Thanks
Jatin

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 19:20:54 von Jim Gibson

On 9/9/10 Thu Sep 9, 2010 9:35 AM, "Jatin"
scribbled:

>
>> File::Find's interface is through its package variables.
> [Jatin] So my subroutine that i use with the File::Find's find method
> has access to the package variables ? Correct me if i am wrong.
>> It is a very
>> bad design and is much regretted, but there is nothing to do about it
>> now.
>>
> [Jatin] Totally agree , i was not able to co-relate this with the
> fundamental idea of passsing references to other subroutines. But its
> ok. Is there any plans to remedy this design in the future releases of
> perl ?

It is unlikely that the File::Find module will be "fixed" (whether or not it
needs fixing is a matter of opinion). If you do not like the interface,
there are several alternatives that have different interfaces available at
CPAN. Go to , enter "File::Find" in the search box,
and explore the possibilities.



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 19:23:42 von Jim Gibson

On 9/9/10 Thu Sep 9, 2010 9:49 AM, "Jatin"
scribbled:

>
>> The callback is printing the value of $File::Find::name (which, again,
>> is set by the File::Find::find function before the callback is
>> called).
>>
>>
>>
> [Jatin] Chas , when you say that the File::Find's find method sets its
> name variable before the callback is called , Do you mean that this
> "name" variable is set by the find function to the currently referenced
> file or directory ? All the three variables namely $_ , $dir and $name
> are set with values corresponding to the currently referenced file ?

The best way to find answers to questions about File::Find or any other
module is to read the documentation that comes with the module.

Enter

perldoc File::Find

at a command-line prompt or



in a browser.



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 19:36:20 von chas.owens

On Thu, Sep 9, 2010 at 12:49, Jatin wrote:
>
>> The callback is printing the value of $File::Find::name (which, again,
>> is set by the File::Find::find function before the callback is
>> called).
>>
>>
>>
>
> [Jatin] Chas , when you say that the File::Find's find method sets its name
> variable before the callback is called , Do you mean that this "name"
> variable is set by the find function to the currently referenced file or
> directory ? All the three variables namely $_ , $dir and $name are set with
> values corresponding to the currently referenced file ?
snip

Yes, if no_chdir is not set, then

$_ is the current file's name with no path
$File::Find::name is the current file's name with the path
$File::Find::dir is the path to the current file

You may want to read the [perldoc File::Find][0].

[0]: http://perldoc.perl.org/File/Find.html#The-wanted-function

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 09.09.2010 19:38:47 von chas.owens

On Thu, Sep 9, 2010 at 12:35, Jatin wrote:
>
>> File::Find's interface is through its package variables.
>
> [Jatin] So my subroutine that i use with the File::Find's find method has
> access to the package variables ? Correct me if i am wrong.
>>
>>   It is a very
>> bad design and is much regretted, but there is nothing to do about it
>> now.
>>
>
> [Jatin] Totally agree , i was not able to co-relate this with the
> fundamental idea of passsing references to other subroutines. But its ok.=
Is
> there any plans to remedy this design in the future releases of perl ?
snip

Well, Perl 6 doesn't have File::Find, so that is a fix in and of
itself. In Perl 5 File::Find will never be "fixed". Too much code
realies on the existing interface, but they are alternatives to
File::Find on CPAN.

--=20
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Callbacks

am 10.09.2010 05:34:44 von Jatin Davey

> Yes, if no_chdir is not set, then
>
> $_ is the current file's name with no path
> $File::Find::name is the current file's name with the path
> $File::Find::dir is the path to the current file
>
> You may want to read the [perldoc File::Find][0].
>
> [0]: http://perldoc.perl.org/File/Find.html#The-wanted-function
>
[Jatin] Thanks Chas , I would definitely explore more on different
modules available at CPAN , that is a great place. Appreciate your help
in making me understand about Callbacks.

Thanks
Jatin

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/