You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Steve Thames <st...@softlife.com> on 2006/03/31 15:41:32 UTC
Nested Interpolation
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?
Re: Nested Interpolation
Posted by Steve Thames <st...@softlife.com>.
Ok, thanks for you help guys. I've got the answer now and its
working.
Sorry for the post here. I was posting a couple of other questions
here and didn't think about this not being related to mod_perl.
Mod_perl is where I'm using it though.
Thanks again.
On Fri, 31 Mar 2006 05:41:32 -0800, Steve Thames <st...@softlife.com>
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?
>
Re: Nested Interpolation
Posted by Ronald J Kimball <rj...@tamias.net>.
On Fri, Mar 31, 2006 at 07:05:41AM -0800, Steve Thames wrote:
> On Fri, 31 Mar 2006 16:33:56 +0200, tomas@tuxteam.de (Tomas Zerolo)
> wrote:
> >
> >You mean something like...
> >
> > my %names = (Bob => 'Robert Brower');
> > my $caption = 'Name: $names{Bob)';
> > print eval "qq|$caption|";
> > die $@ if $@
> >
> >giving:
> >
> > syntax error at (eval 1) line 1, near "Bob)"
> > Missing right curly or square bracket at (eval 1) line 1, within string
> >
> >HTH
> >-- tomas
>
> Exactly right. Except $@ is empty in this construction--no error is
> reported but the eval fails.
>
> It has something to do with the way perl uses the setjmp and longjmp c
> functions. I was reading something about that.
>
> I'm trying to trap the syntax error.
It works for me. I get the same result as Tomas. $@ contains the expected
error message.
But what does this have to do with modperl??
Ronald
Re: Nested Interpolation
Posted by Steve Thames <st...@softlife.com>.
On Fri, 31 Mar 2006 16:33:56 +0200, tomas@tuxteam.de (Tomas Zerolo)
wrote:
>On Fri, Mar 31, 2006 at 05:41:32AM -0800, Steve Thames wrote:
>> Consider this:
>>
>> my %names = (Bob => 'Robert Brower');
>> my $caption = 'Name: $names{Bob)';
>> print eval "qq|$caption|";
>[...]
>> Does anyone have any idea how to capture the syntax error in a case
>> like this?
>
>
>You mean something like...
>
> my %names = (Bob => 'Robert Brower');
> my $caption = 'Name: $names{Bob)';
> print eval "qq|$caption|";
> die $@ if $@
>
>giving:
>
> syntax error at (eval 1) line 1, near "Bob)"
> Missing right curly or square bracket at (eval 1) line 1, within string
>
>HTH
>-- tomas
Exactly right. Except $@ is empty in this construction--no error is
reported but the eval fails.
It has something to do with the way perl uses the setjmp and longjmp c
functions. I was reading something about that.
I'm trying to trap the syntax error.
Re: Nested Interpolation
Posted by Tomas Zerolo <to...@tuxteam.de>.
On Fri, Mar 31, 2006 at 05:41:32AM -0800, Steve Thames wrote:
> Consider this:
>
> my %names = (Bob => 'Robert Brower');
> my $caption = 'Name: $names{Bob)';
> print eval "qq|$caption|";
[...]
> Does anyone have any idea how to capture the syntax error in a case
> like this?
You mean something like...
my %names = (Bob => 'Robert Brower');
my $caption = 'Name: $names{Bob)';
print eval "qq|$caption|";
die $@ if $@
giving:
syntax error at (eval 1) line 1, near "Bob)"
Missing right curly or square bracket at (eval 1) line 1, within string
HTH
-- tomas
Re: Nested Interpolation
Posted by Steve Thames <st...@softlife.com>.
On Fri, 31 Mar 2006 08:48:20 -0500, Michael Peters
<mp...@plusthree.com> 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.
Re: Nested Interpolation
Posted by Michael Peters <mp...@plusthree.com>.
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.
--
Michael Peters
Developer
Plus Three, LP
Re: Nested Interpolation
Posted by "Hans Meier (John Doe)" <se...@tele2.ch>.
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: $@\n" 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
Re: Nested Interpolation
Posted by "Hans Meier (John Doe)" <se...@tele2.ch>.
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: $@\n" 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...