Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl's Data::Dumper shows objects instead of values

 foreach my $row (1..$end) 
 { 
  foreach my $col (3..27 ) 
  { 
    # skip empty cells 
    next unless defined 
    $worksheet->Cells($row,$col)->{'Value'}; 

    # print out the contents of a cell  
    $var = $worksheet->Cells($row,$col)->{'Value'};     
    push @dates, $var;  

    print $var; #this prints the value just fine
  } 
 }  

my %hash;
$hash{'first'} = \@dates;
print Dumper \%hash; #This prints object information 

I am using the module OLE for Perl and every value I get from my worksheet and print $var then I get the expected value, but when I put everything into a hash it prints:

'first' => [
bless( do{\(my $o = 15375916)}, 'OLE::Variant'), 
bless( do{\(my $o = 15372208)}, 'OLE::Variant'),

And so forth. I must not understand something about hashes, because I'm really stumped here.

like image 969
Melissa Avatar asked Feb 01 '11 17:02

Melissa


2 Answers

push @dates, $var pushes an OLE::Variant object onto your @dates array, while print $var calls the implicit OLE::Variant method to convert the object to a string.

If you also want @dates to just contain the underlying string values and not the objects themselves, say

push @dates, "$var";

which will stringify the date object before putting it into the @dates array.

like image 165
mob Avatar answered Oct 12 '22 23:10

mob


The values returned by the $worksheet->Cells($row,$col)->{'Value'} call are objects that are mostly C/C++ in nature, and Perl only has a handle on the object, represented by a memory location (which you see in the dump as a large integer). Many CPAN modules that wrap underlying C/C++ libraries behave the same way (XML::LibXML is on example that pops to mind). The short answer is, this is the object, and it is all you can see by means of Data::Dumper unfortunately. They are essentially blessed scalar references, and all operations on them are through methods, not through the actual value of the underlying reference itself.

like image 37
rjray Avatar answered Oct 13 '22 00:10

rjray