Perl 的 Data::Dumper 显示对象而不是值

发布于 2024-10-15 19:10:48 字数 729 浏览 7 评论 0原文

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唐婉 2024-10-22 19:10:48

push @dates, $varOLE::Variant 对象推送到 @dates 数组中,同时 print $var > 调用隐式 OLE::Variant 方法将对象转换为字符串。

如果您还希望 @dates 仅包含基础字符串值而不包含对象本身,请说

push @dates, "$var";

这将在将日期对象放入 @dates 数组之前将其字符串化。

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.

谎言 2024-10-22 19:10:48

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文