Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Win32::OLE and taint

by Michael Roberts (Sexton)
on Dec 01, 2013 at 16:01 UTC ( #1065154=perlquestion: print w/replies, xml ) Need Help??

Michael Roberts has asked for the wisdom of the Perl Monks concerning the following question:

OK, this is just driving me crazy.

I'm calling Word Automation using Win32::OLE, and to open a file I use e.g.:

use Win32::OLE qw(CP_UTF8); $Win32::OLE::CP = CP_UTF8; my $W = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application') || die ("couldn't create Word object"); my $document = $W->Documents->Open("my.doc");

That's it. If I run perl -T, though, I get the "Not a Win32::OLE object" error at the line the object was created when the object goes out of scope and gets freed.

If I don't get the document back as a return value (i.e. I just look for the member of the Documents list that got created when I opened it and ignore the return value from Open), then I get the error at the line where the open is called. If I don't use the return value, but instead allow the variable to go out of scope after doing some other stuff, I get the error with the first line of the scope. And if I assign something else to the variable holding the weird object, I get the error with the line number of the assignment.

So, ignoring for the moment the fact that the line reported reflects the scope of the variable, which is neat but pretty misleading when you're trying to find an error, why the h-e-double-toothpicks does the error not appear when I don't run perl with the taint flag??!??!? I don't even know how OLE.xs knows about the tainting or why it would make a difference to the garbage collector or, really, anything at all. I would like to be able to test and publish a CPAN module with this code without seeing these freaky error messages all over the place with information that doesn't appear to matter to me - because the object in question works just fine and is a perfectly OK Word OLE object.

Help me here.

Replies are listed 'Best First'.
Re: Win32::OLE and taint
by Generoso (Prior) on Dec 01, 2013 at 20:37 UTC

    This works for me. Try it.

    use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 2; # Throw Errors, I'll catch them my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); $Word->{'Visible'} = 1; $Word->Documents->Add || die("Unable to create document ", Win32::OLE- +>LastError()); my $MyRange = $Word->ActiveDocument->Content; my $mytxt = "Some Random Text"; # I'll fill this in later # add a table that is the header portion, 1 column by 1 row $Word->ActiveDocument->Tables->Add({ Range => $MyRange, NumRows => 1, NumColumns => 1, }); $Word->Selection->TypeText ({ Text => $mytxt}); $Word->Selection->MoveRight({Count => 1}); $Word->Selection->TypeText ({ Text => "A little more text"});
Re: Win32::OLE and taint
by Generoso (Prior) on Dec 01, 2013 at 23:45 UTC
    use Win32::OLE qw(CP_UTF8); use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::CP = CP_UTF8; my $W = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application') || die ("couldn't create Word object"); $W->{'Visible'} = 1; my $document = $W->Documents->Open("D:\\mydoc.doc") || die("Unable to open document ", Win32::OLE->LastError()); print $document."\n";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1065154]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2022-05-28 23:41 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.