The short answer is that you can't do this using eval the way you want to because Perl needs to know at compile time whether a variable you're using is a global or a lexical, so it doesn't make sense to declare lexical variables in eval'd code and then use them in non-eval'd code in the same block. But I don't think you need anything so drastic.

I would start at the most basic level. It sounds like what you're trying to do can be described simply as "assign some lexical variables to values passed in through a hash, and make sure they're defined." The simplest way to do the assignment part is

my ($self, %values) = @_; # we're being passed a hash so let's treat +it like a hash my ($tree, $messageNum, $messageText) = @values{qw/tree messageNum mes +sageText/};
So far so good. But we also need to insure that a) the parameters are all there, b) there are no extraneous parameters, and c) they all have defined values. This we can abstract into a subroutine like this:
sub validateParams { my ($data, @paramNames) = @_; die "first argument to validateParams must be a hash" unless ref +$data eq 'HASH'; my $expect_count = @paramNames; my $actual_count = keys %$data; die "incorrect number of parameters - expected $expect_count, got + $actual_count" unless $expect_count == $actual_count; for my $name (@paramNames) { die "parameter $name missing or undefined" unless defined $da +ta->{$name}; } }
and we can call it like this:
{ my @paramNames = qw/tree messageNum messageText/; sub new { my ($self, %values) = @_; validateParams(\%values, @paramNames); my ($tree, $messageNum, $messageText) = @values{@paramNames}; } }
I put it in an enclosing block like that so that each subroutine can have its private list of required parameter names set up in advance.

There are also some modules on CPAN for this such as Params::Validate.


In reply to Re^3: Usage of 'my' in 'eval' by Errto
in thread Usage of 'my' in 'eval' by Anonymous Monk

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.