DrWhy has asked for the wisdom of the Perl Monks concerning the following question:
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!'.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Confusion with eval
by eibwen (Friar) on May 04, 2005 at 17:17 UTC | |
|
Re: Confusion with eval
by DrWhy (Chaplain) on May 04, 2005 at 18:06 UTC |