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

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

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

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

Randal L. Schwartz

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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














7.7 Displaying Recursively Defined Data


The Dumper routine
of Data::Dumper displays the output nicely, but
what if you don't like the format being used? You
can write a routine to display the data. Again, for recursively
defined data, a recursive subroutine is usually the key.

To dump the data, you need to know the name of the directory at the
top of the tree because that's not stored within the
structure:

sub dump_data_for_path {
my $path = shift;
my $data = shift;
if (not defined $data) { # plain file
print "$path\n";
return;
}
...
}

For a plain file, dump the pathname; for a directory,
$data is a hash reference. Let's
walk through the keys and dump the values:

sub dump_data_for_path {
my $path = shift;
my $data = shift;
if (not defined $data) { # plain file
print "$path\n";
return;
}
my %directory = %$data;
for (sort keys %directory) {
dump_data_for_path("$path/$_", $directory{$_});
}
}

For each element of the directory, you pass a path consisting of the
incoming path followed by the current directory entry, and the data
pointer is either undef for a file or a
subdirectory hash reference for another directory. You can see the
results by running:

dump_data_for_path(".", data_for_path("."));

Again, this is more interesting in a directory that has
subdirectories, but the output should be similar to:

find . -print

from the shell prompt.



/ 199