- 论坛徽章:
- 0
|
With an eval, you should be especially careful to remember what's being looked at when:
1. eval $x; # CASE 1
2. eval "$x"; # CASE 2
3.
4. eval '$x'; # CASE 3
5. eval { $x }; # CASE 4
6.
7. eval "\$$x++"; # CASE 5
8. $$x++; # CASE 6
Cases 1 and 2 above behave identically: they run the code contained in the variable $x. (Although case 2 has misleading double quotes making the reader wonder what else might be happening (nothing is).) Cases 3 and 4 likewise behave in the same way: they run the code '$x' , which does nothing but return the value of $x. (Case 4 is preferred for purely visual reasons, but it also has the advantage of compiling at compile-time instead of at run-time.) Case 5 is a place where normally you would like to use double quotes, except that in this particular situation, you can just use symbolic references instead, as in case 6. |
|