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

2019-08-10 22:51发布

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条回答
做个烂人
2楼-- · 2019-08-10 23:38

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.

查看更多
登录 后发表回答