Learning Perl Objects, References amp;amp; Modules [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Learning Perl Objects, References amp;amp; Modules [Electronic resources] - نسخه متنی

Randal L. Schwartz

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید














6.3 Callbacks


A subroutine reference is often
used for a callback. A callback defines what to
do when a subroutine reaches a particular place in an algorithm.

For example, the
File::Find module exports a
find subroutine that can efficiently walk through
a given filesystem hierarchy in a fairly portable way. In its
simplest form, you give the find subroutine two
parameters: a starting directory and "what to
do" for each file or directory name found
recursively below that starting directory. The "what
to do" is specified as a subroutine reference:

use File::Find;
sub what_to_do {
print "$File::Find::name found\n";
}
my @starting_directories = qw(.);
find(\&what_to_do, @starting_directories);

In this case,
find starts at the current directory
(.) and locates each file or directory. For each
item, a call is made to the subroutine what_to_do(
)
, passing it a few documented values through global
variables. In particular, the value of
$File::Find::name is the item's
full pathname (beginning with the starting directory).

In this case,
you're passing both data (the list of starting
directories) and behavior as parameters to the
find routine.

It's a bit
silly to invent a subroutine name just to use the name only once, so
you can write the previous code using an anonymous subroutine, such
as:

use File::Find;
my @starting_directories = qw(.);
find(
sub {
print "$File::Find::name found\n";
},
@starting_directories,
);



/ 199