Hans Meier (John Doe) am Freitag, 31. März 2006 18.37:
> Steve Thames am Freitag, 31. März 2006 15.41:
> > 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?
>
> What about:
>
> ===
> #!/usr/bin/perl
> use strict;
> #use warnings;
>
> my %names = (Bob => 'Robert Brower');
> my $caption = 'Name: $names{Bob)';
> my $caption2 = 'Name: $names{Bob}';
>
> eval "eval {qq|$caption|}";
> print "Error: [EMAIL PROTECTED]" if $@;
>
> eval "eval {qq|$caption2|}";
> print "Error: $@" if $@;
> print "going on...\n";
> ===
>
> $ ./script.pl
> Error: syntax error at (eval 1) line 1, near "Bob)"
> Missing right curly or square bracket at (eval 1) line 1, within string
>
> going on...

Or even without the quotes around the inner evals, producing:

$ ./script.pl
Error: syntax error at (eval 1) line 2, near "Bob)
"
Missing right curly or square bracket at (eval 1) line 2, at end of line

going on...


Sorry for hitting "send" twice :-(

Hans

Reply via email to