Perl 的 Data::Dumper 显示对象而不是值
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
我正在使用 Perl 的 OLE 模块,以及从工作表中获得的每个值并打印 $var 然后我得到预期值,但是当我将所有内容放入哈希中时,它会打印:
'first' => [
bless( do{\(my $o = 15375916)}, 'OLE::Variant'),
bless( do{\(my $o = 15372208)}, 'OLE::Variant'),
等等。我一定不明白有关哈希的东西,因为我真的被难住了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
push @dates, $var
将OLE::Variant
对象推送到@dates
数组中,同时print $var
> 调用隐式OLE::Variant
方法将对象转换为字符串。如果您还希望 @dates 仅包含基础字符串值而不包含对象本身,请说
这将在将日期对象放入
@dates
数组之前将其字符串化。push @dates, $var
pushes anOLE::Variant
object onto your@dates
array, whileprint $var
calls the implicitOLE::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, saywhich will stringify the date object before putting it into the
@dates
array.$worksheet->Cells($row,$col)->{'Value'}
调用返回的值本质上主要是 C/C++ 对象,而 Perl 只有对象的句柄,由内存位置表示(您在转储中将其视为一个大整数)。许多包装底层 C/C++ 库的 CPAN 模块的行为方式相同(XML::LibXML 就是我想到的示例)。简短的回答是,这是对象,不幸的是,它是您通过 Data::Dumper 所能看到的全部内容。它们本质上是受祝福的标量引用,对它们的所有操作都是通过方法,而不是通过底层引用本身的实际值。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.