Eval Errors
From: James Edward Gray II (james_at_grayproductions.net)
Date: 02/23/04
- Next message: Charles K. Clarkson: "RE: error msg : can't use string as an ARRAY ref"
- Previous message: Rob Dixon: "Re: Help with end of line charaters"
- Next in thread: James Edward Gray II: "Re: Eval Errors"
- Reply: James Edward Gray II: "Re: Eval Errors"
- Reply: James Edward Gray II: "Re: Eval Errors"
- Maybe reply: Jeffrey N Dyke: "Re: Eval Errors"
- Maybe reply: Randy W. Sims: "Re: Eval Errors"
- Maybe reply: James Edward Gray II: "Re: Eval Errors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: Perl Beginners <beginners@perl.org> Date: Mon, 23 Feb 2004 10:35:28 -0600
I'm having a heck of a time getting eval() to obey me this morning and
I thought I would see if I can get a fresh perspective from someone.
Okay, I'm passing a server I wrote some Perl code I want it to eval().
This morning, during testing, I fed it some bad code. It's supposed to
handle that by doing nothing and giving me the error that was
generated, so I can fix it and try again. It is doing these things,
but it's generating some warnings on the server, I want to silence.
Here's what the relevant code looks like:
my %details = eval $replacements{ACTION_DETAILS};
if ($@) {
# tell me what went wrong... (working)
}
($replacements{ACTION_DETAILS} is supposed to eval() to a hash
initializing list, naturally.)
When I run this, feeding it the error containing code, the server spews
the following warnings:
Odd number of elements in hash assignment at libs/Improv/Director.pm
line 29.
Use of uninitialized value in list assignment at
libs/Improv/Director.pm line 29.
(libs/Improv/Director.pm is the module that contains the code shown
above and line 29 is the eval() line of the above snippet.)
That much makes sense to me. eval() is returning undef since the code
didn't compile and that's tripping both of the above warnings. Got it.
So I tried a simple fix:
my %details = eval($replacements{ACTION_DETAILS}) || ();
And to my surprise, that made things much worse. The server now
complains:
Bareword found where operator expected at (eval 9) line 1, near "'sub {
my $self = shift; my $actor = shift; $actor->sense("The rose has the
crisp smell of shaved ice.\n"); if ($self->{Owner}->isa('Improv::Set"
(Missing operator before Improv::Set?)
String found where operator expected at (eval 9) line 1, near
"Improv::Set')) { $_->sense("$actor->{Name} leans over $self->{Name}
and inhales.\n") foreach $self->{Owner}->get_actors($actor); return 1;
}'"
That slew of errors is complaining about the code I passed eval().
Those are the errors I made in the code I handed the server. I can fix
the error, that's not the issue. I don't understand why the server is
complaining about what happened inside an eval().
So I tried some other fixes:
my %details;
{
no warnings;
%details = eval $replacements{ACTION_DETAILS};
}
And:
my %details;
eval "%details = $replacements{ACTION_DETAILS}";
Both of these give me the second set of errors I posted above, the
errors that keep the code in $replacements{ACTION_DETAILS} from
compiling. That confused me even more. How the heck is shutting off
warnings increasing the complaints?
I believe I have a solution now. I'm going to try switching the way
the system works to eval() to a hash reference, instead of a hash. I
expect that to silence the two original warnings caused by the undef
being returned.
However, I don't understand why the server was complaining about the
the code inside the eval() with all of my attempted fixes. My
understanding was that eval() should trap the errors and I'll find them
in $@. If anyone can enlighten me as to what the heck is going one
here, I would be most grateful.
Thanks.
James
- Next message: Charles K. Clarkson: "RE: error msg : can't use string as an ARRAY ref"
- Previous message: Rob Dixon: "Re: Help with end of line charaters"
- Next in thread: James Edward Gray II: "Re: Eval Errors"
- Reply: James Edward Gray II: "Re: Eval Errors"
- Reply: James Edward Gray II: "Re: Eval Errors"
- Maybe reply: Jeffrey N Dyke: "Re: Eval Errors"
- Maybe reply: Randy W. Sims: "Re: Eval Errors"
- Maybe reply: James Edward Gray II: "Re: Eval Errors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|