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

Hi, I am working on a code snippet which tends to give me an error...
Internal error: eval failed: Global symbol "@fields" requires explicit package name at (eval 143357592) line 2.
Global symbol "@fields" requires explicit package name at (eval 143357592) line 4.
Global symbol "@fields" requires explicit package name at (eval 143357592) line 5.

Below is the code snippet:
my $evalstr = <<EOEVAL; my \@fields = (\$val =~ /\$ivalue/$modifiers); if (\@fields) { for (\$j = 0; \$j < \@fields; \$j++) { \$self->{_regex_vars}[\$c]{\$tag}[\$j+1] = \$fields[\$j] } 1 } else { -1 } EOEVAL

I tried with different permutations and combinations to generate the above error by pasting this code snippet into a individual perl module, but was not able to get the error. Can someone explain in which conditions will i get above mentioned error in the mentioned code snippet? How can i simulate the error?
Thanks, ShekharL

Replies are listed 'Best First'.
Re: Global symbol
by dave_the_m (Monsignor) on Jun 19, 2008 at 14:05 UTC
    I'd be suspicious of odd text in $modifiers messing up the parsing of the code, making the lexical @files no longer in scope.

    Do you have the ability to print out the value of $evalstr when the eval fails?

    Dave.

Re: Global symbol
by psini (Deacon) on Jun 19, 2008 at 14:02 UTC
    my \@fields = (\$val =~ /\$ivalue/$modifiers);

    You can't, AFAIK, assign a value to the reference of an array.

    perl don't tell you that because it fails on the use of undefined var @fields

    I think there are too many backslashes in your code, but I can't really understand what you are trying to do

    Careful with that hash Eugene.

      Perhaps you shouldn't try and offer advice if you don't understand things . . .

      At any rate, he's building up a string to pass to eval using a heredoc. Since he hasn't explicitly wrapped the terminator token in single quotes it will interpolate variables like a double quoted string. And since @array will interpolate in a double quoted string he's backwhacking the initial @ since he wants the literal text my @fields = ($val =~ /$ivalue/...); (... being the interpolated contents of $modifiers). What actually gets passed to eval doesn't have any extraneous backwhacks in it. The next obvious step in debugging this is would be to print out the code that's misbehaving when being passed to eval (or printing the code if $@ is set after the eval indicating problems).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        You are perfectly right :(

        I didn't notice the first line of the code snippet and read the remaining completely out of context. Sorry.

        Careful with that hash Eugene.

        Thanks a lot Fletch for your response. I will check out with the actual contents that are being passed to this code snippet.