Now, can any monk more wise or at least more educated than I, explain why?

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:

#!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";
Instead of "success!" I got:
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.
Changing the eval line like so:
my $result = eval {$sub->()};
fixes the problem.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.