[comp.lang.perl] I ${$a}

stef@zweig.sun (Stephane Payrard) (10/11/90)

I would expect a expression like ${$a} to evaluate from
inside out:
It is not the case as demonstrated by the following
test.


% perl -e '$b=2;$a="b";print ${$a};'
 
I would expect ${$a} to evaluate to 1 the following way:

  ${$a} -> ${"b"} -> $b -> 1


What is the canonic way to write an expression which
behaves like I exprected (I guess it imply to use `eval').

This lead to RFE:
   supporting this kind of multilevel evalation of string:
         -If an expression contains the construct  ${expression},
          this construct is evaluated and replaced by its value 
          before the string contained it is evaluated.


        stef
--
Stephane Payrard -- stef@eng.sun.com -- (415) 336 3726
SMI 2550 Garcia Avenue M/S 10-09  Mountain View CA 94043

                     
                     

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (10/12/90)

In article <STEF.90Oct11015128@zweig.sun> stef@zweig.sun (Stephane Payrard) writes:
: 
: I would expect a expression like ${$a} to evaluate from
: inside out:
: It is not the case as demonstrated by the following
: test.
: 
: 
: % perl -e '$b=2;$a="b";print ${$a};'
:  
: I would expect ${$a} to evaluate to 1 the following way:
: 
:   ${$a} -> ${"b"} -> $b -> 1
: 
: 
: What is the canonic way to write an expression which
: behaves like I exprected (I guess it imply to use `eval').
: 
: This lead to RFE:
:    supporting this kind of multilevel evalation of string:
:          -If an expression contains the construct  ${expression},
:           this construct is evaluated and replaced by its value 
:           before the string contained it is evaluated.

Multi-level evaluation is one of the things that makes shell programming
such a pain.  I'm not gonna do that to Perl.  You'll note "text`command`text"
doesn't work either.  I'd rather force you to make a few extra temporary
variables than have people writing "Perl scripts out of hell".  They're bad
enough coming out of purgatory.

In the ${...} construct, the {} characters only perform grouping and quoting.
No evaluation of anything is implied other than a possible subscript, e.g.
inside ${foo[$bar]} or ${foo{$bar}}.  In particular, in ${$a}, the second
$ is only allowed accidentally because ${$} must be allowed as a synonym
for $$, so it doesn't check the first character to make sure it's alphanumeric
like it does the rest of the characters.  Thus, the way it currently stands,
you can say ${^Afoo} (where ^A is a real control/a, but you can't say ${foo^A}.
I wouldn't, of course, depend on this for anything if I were you, since
it's undocumented and may change.  In fact, once upon a time, *any* sequence
of characters was allowed inside the {}, but it was tightened up some when
we instituted ${foo[$bar]}.

Larry