打印IO :: All与Data :: Dumper的返回? [英] Printing the return of IO::All with Data::Dumper?

查看:114
本文介绍了打印IO :: All与Data :: Dumper的返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码段:

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' );
...

我希望我将转储各个对象的所有字段,而不是;起初,我以为这是一个引用,所以我认为如果取消引用该变量,则将打印这些字段-但我意识到我真的不知道如何取消引用它.

I expected I would get the all the fields of the respective objects dumped, instead; at first, I thought this was a reference, so I thought the fields would be printed if I dereference the variable - but I realized I don't really know how to dereference it.

那么-如何使用相同的for my ...循环打印出@contents的所有字段和内容?

So - how can I print out all the fields and contents of the @contents, using the same kind of for my ... loop?

推荐答案

您可以执行以下操作:

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

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

但是您应该认真考虑这样做是否是个好主意.面向对象编程的核心原则之一是封装.您不必关心受祝福对象的胆量,而应仅通过它提供的方法与之交互.

But you should seriously consider whether doing this is a good idea. One of the core tenets of object-oriented programming is encapsulation. You should not care about the guts of a blessed object - you should interact with it only via the methods it provides.

这篇关于打印IO :: All与Data :: Dumper的返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆