开发者

Converting Win32::OLE::Variant to perl variable?

开发者 https://www.devze.com 2023-03-28 00:24 出处:网络
When I extract text from an excel file it comes as Win32::OLE::Variant=SCALAR(0x1214f94). How do I convert this to a Perl variable? When I try to use the value function is just prints @data = @{$dataH

When I extract text from an excel file it comes as Win32::OLE::Variant=SCALAR(0x1214f94). How do I convert this to a Perl variable? When I try to use the value function is just prints @data = @{$dataHash{$header}};

my ($refs,$rows,$header) = &extract;

my %dataHash = %{$refs};
print "Keys:",keys %dataHash,"\n";
my @headers =  @{$header};

my @test = @{$dataHash{'Date'}};

foreach my $scalar (@test){
    print $scal开发者_StackOverflow社区ar;
}

foreach my $header (@headers){
    print "Checking Header: $header\n";

    @data = @{$dataHash{$header}};

    print $data;
    foreach my $scalar (@data){
        print FH "$scalar\n";
    }
}

UPDATE:

All I needed to do was enter use Win32::OLE::Variant and Win32::OLE::Variant=SCALAR(0x1214f94) became a readable answer.


As a general rule, when you get a value such as SCALAR(0x1214f94), you are printing a reference/address, not a value. You can dereference by adding a dollar sign in front of the variable:

$a = \$b;
$b = 1;
print $a;   # prints something like SCALAR(0x1214f94)
print $$a;  # prints 1


In this case, what you are getting is Win32::OLE::Variant object, that is often returned from OLE calls when more complex type than text is needed. See methods listed in linked page and choose appropriate to get perl representation.

For instance, when you have a date in the Variant object, you can get it as string like this:

my $string = $date->Date("yyyy-MM-dd");
0

精彩评论

暂无评论...
验证码 换一张
取 消