I was trying to make a little wrapper routine for calls to Win32::OLE methods to combine them with error checks, so I don't have to rewrite the error checking code for each call.
Here is a baby example using my first attempt at OLECheck:
Instead of "success!" I got:#!c:/sw/perl/bin/perl.exe use warnings; use strict; use Win32::OLE; our $project; sub OLECheck (&$) { my ($sub,$msg) = @_; my $result = eval &$sub; die "Couldn't $msg: $@" if $@; my $error = Win32::OLE->LastError(); die "Couldn't $msg: $error" if $error; $result; } OLECheck {$project = Win32::OLE->new("ISWiAutomation9.ISWiProject");} "instantiate the Developer Automation Interface"; print "success!\n";
Changing the eval line like so:Couldn't instantiate the Developer Automation Interface: Can't modify +constant item in scalar assignment at (eval 2) line 2, at EOF Bareword "Win32::OLE" not allowed while "strict subs" in use at (eval +2) line 1.
fixes the problem.my $result = eval {$sub->()};
Even though my immediate problem is solved, I'm still curious as to why the original failed in the way that it did.
--DrWhy
"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."
Retitled by Steve_p from 'I solved my problem!'.
In reply to I solved my Problem! (Confusion with eval) by DrWhy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |