1

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?

sdaau
  • 36,975
  • 46
  • 198
  • 278

1 Answers1

3

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.

tobyink
  • 13,478
  • 1
  • 23
  • 35
  • Many thanks for that, @tobyink ! Thanks also for the warning: I thought the Dumper would reveal both "methods" and "properties"; and I just wanted to see what sort of fields are present first; now I realize only "properties" are printed, and that I should look up the corresponding methods for them. Thanks again - cheers! – sdaau Jul 12 '14 at 21:30
  • 1
    `Data::Printer` will output the methods (or what it thinks are methods) of objects. – friedo Jul 12 '14 at 22:09
  • Somewhat annoyingly Data::Printer doesn't include inherited methods though. (Nor methods provided by Moose/Moo/etc roles.) – tobyink Jul 13 '14 at 13:08