-->

打印的IO ::所有与数据::自卸车的回报?(Printing the return of IO::

2019-09-29 06:43发布

考虑这个片断:

use Data::Dumper;

@targetDirsToScan = ("./");
use IO::All;
$io = io(@targetDirsToScan);                # Create new directory object
@contents = $io->all(0);                    # Get all contents of dir
for my $contentry ( @contents ) {
  print Dumper($contentry) ."\n";
}

这将打印类似:

$VAR1 = bless( \*Symbol::GEN298, 'IO::All::File' );
$VAR1 = bless( \*Symbol::GEN307, 'IO::All::Dir' );
$VAR1 = bless( \*Symbol::GEN20, 'IO::All::File' );
...

我希望我会被人抛弃,而不是各个对象的所有字段; 起初,我认为这是一个参考,所以我想等领域会,如果我取的可变印刷 - 但我意识到我真的不知道如何取消对它的引用。

所以-我怎么能打印出的各个领域和内容@contents ,使用相同类型的for my ...循环?

Answer 1:

你可以这样做:

use Data::Dumper;
use IO::All;

$io = io('/tmp');
for my $file ( $io->all(0) ) {
   print Dumper \%{*$file};
}

但是你应该认真考虑是否做,这是一个好主意。 一个面向对象编程的核心原则之一就是封装 。 你不应该在乎祝福对象的胆量 - 你只应通过其提供的方法与它进行交互。



文章来源: Printing the return of IO::All with Data::Dumper?