Printing the return of IO::All with Data::Dumper?

元气小坏坏 提交于 2019-12-11 09:35:15

问题


Consider this snippet:

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";
}

This prints something like:

$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.

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


回答1:


You can do this:

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.



来源:https://stackoverflow.com/questions/24717210/printing-the-return-of-ioall-with-datadumper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!