On Fri, 31 Mar 2006 08:48:20 -0500, Michael Peters
<[EMAIL PROTECTED]> wrote:
>
>
>Steve Thames wrote:
>> Consider this:
>>
>> my %names = (Bob => 'Robert Brower');
>> my $caption = 'Name: $names{Bob)';
>> print eval "qq|$caption|";
>>
>> If you can't see it, there is a syntax error in $caption: closing
>> paren ) instead of brace }. The eval will produce no $@ and will
>> return the empty string.
>>
>> As screwy as this looks, I have a very good reason for using this
>> capability. I have written a powerful code generation tool that
>> relies heavily on this.
>>
>> Does anyone have any idea how to capture the syntax error in a case
>> like this?
>
>It's not a syntax error, per say. Perl's string parser will look in
>double quoted strings for something that looks like a variable. If it
>couldn't be interpreted as a valid variable outside of the string it is
>ignored. It can't possibly through an error since there are a million
>different things that might look kinda like a variable, but aren't really.
Consider this:
my %names = (Bob => 'Robert Brower');
print eval "Name: $names{Bob)";
This produces:
syntax error at test.pl line 3, near "Bob)"
Missing right curly or square bracket at test.pl line 5, within string
In this construction:
my %names = (Bob => 'Robert Brower');
my $caption = 'Name: $names{Bob)';
print eval "qq|$caption|";
I am simply nesting the interpolation. The eval returns nothing and
$@ is empty. The syntax error is still happening which is why the
eval fails. I simply want to capture the error and report it.