diff options
author | Gurusamy Sarathy <gsar@engin.umich.edu> | 1997-07-01 10:17:12 +1200 |
---|---|---|
committer | Tim Bunce <Tim.Bunce@ig.co.uk> | 1997-08-07 00:00:00 +1200 |
commit | b8a4b1bed690d5e67ab7dfcb2ddfb2aa59ccefd7 (patch) | |
tree | 53fece920cb2f78f7e13cc2d8e934b176880f5c0 /t | |
parent | de939608bd9b236d9170fa8c2bcd0a8b397bba0a (diff) | |
download | perl-b8a4b1bed690d5e67ab7dfcb2ddfb2aa59ccefd7.tar.gz |
Eval fails in certain situations (eval "{'...")
On Sun, 20 Jul 1997 16:02:05 MDT, Dave Carrigan wrote:
>Eval will fail in the following situation:
>
>- eval'ing a string
>- the string represents an anonymous hash
>- the first key of the anon hash is single quoted, and contains an
> embedded single quote escaped with a backslash
>- using the form `` $ref = eval $string ''
>
>The MLDBM module uses this form of eval all the time, so the above
>situation actually has the potential to occur quite often.
>$string2 = "{'a\\'' => 'foo', 'b' => 'bar', 'c' => 'bat'}";
That is one of the cases where the note in perlref (about
disambiguating braces not preceded by anything else) applies.
However, in this particular case, the code that recognizes if
a literal string is the first thing inside the curlies is not
doing a thorough job of it. The attached patch should cure
it.
Note that you'll still need to write C<eval "{ $a => 'foo' }">
as C<eval "+{ $a => 'foo' }"> if you want it to evaluate as a
hashref. Perl only auto-disambiguates if the first thing in
the curlies is a literal string followed by a comma or =>.
I'll change MLDBM to conform, for the next release.
p5p-msgid: 199707211753.NAA14940@aatma.engin.umich.edu
Diffstat (limited to 't')
-rwxr-xr-x | t/comp/term.t | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/t/comp/term.t b/t/comp/term.t index b248e9b161..eb9968003e 100755 --- a/t/comp/term.t +++ b/t/comp/term.t @@ -4,7 +4,7 @@ # tests that aren't important enough for base.term -print "1..14\n"; +print "1..22\n"; $x = "\\n"; print "#1\t:$x: eq " . ':\n:' . "\n"; @@ -33,3 +33,38 @@ if ("$foo[1]b" eq "2b") { print "ok 12\n";} else {print "not ok 12\n";} if ("@foo[0..1]b" eq "1 2b") { print "ok 13\n";} else {print "not ok 13\n";} $" = '::'; if ("@foo[0..1]b" eq "1::2b") { print "ok 14\n";} else {print "not ok 14\n";} + +# test if C<eval "{...}"> distinguishes between blocks and hashrefs + +$a = "{ '\\'' , 'foo' }"; +$a = eval $a; +if (ref($a) eq 'HASH') {print "ok 15\n";} else {print "not ok 15\n";} + +$a = "{ '\\\\\\'abc' => 'foo' }"; +$a = eval $a; +if (ref($a) eq 'HASH') {print "ok 16\n";} else {print "not ok 16\n";} + +$a = "{'a\\\n\\'b','foo'}"; +$a = eval $a; +if (ref($a) eq 'HASH') {print "ok 17\n";} else {print "not ok 17\n";} + +$a = "{'\\\\\\'\\\\'=>'foo'}"; +$a = eval $a; +if (ref($a) eq 'HASH') {print "ok 18\n";} else {print "not ok 18\n";} + +$a = "{q,a'b,,'foo'}"; +$a = eval $a; +if (ref($a) eq 'HASH') {print "ok 19\n";} else {print "not ok 19\n";} + +$a = "{q[[']]=>'foo'}"; +$a = eval $a; +if (ref($a) eq 'HASH') {print "ok 20\n";} else {print "not ok 20\n";} + +# needs disambiguation if first term is a variable +$a = "+{ \$a , 'foo'}"; +$a = eval $a; +if (ref($a) eq 'HASH') {print "ok 21\n";} else {print "not ok 21\n";} + +$a = "+{ \$a=>'foo'}"; +$a = eval $a; +if (ref($a) eq 'HASH') {print "ok 22\n";} else {print "not ok 22\n";} |