in reply to Nested Hash Dereferencing Syntax

Perl sees %$testhash, but doesn't know of any $testhash. You want %{ $testhash{ "Tier 1" } }

Replies are listed 'Best First'.
Re^2: Nested Hash Dereferencing Syntax
by jodv (Initiate) on Feb 24, 2009 at 17:31 UTC
    Nice. I tried parens, but not curlies, in my attempt to make sure Perl was evaluating the value of $testhash{ "Tier 1" } before trying to dereference.
    Do you know why curlies, and not parens, are necessary here? Just curious.
    Thanks for your help!
      Here is an explanation of why parens cannot be used there.

      If you could write %($hash{$key}) then you should expect to be able to write %($hash{$key}){$other_key}. But perlvar tells us that $( is a built-in special variable, so correctly knowing how to parse that would be very, very hard.

      Incidentally %( is a valid Perl hash, though it is not used for anything and accessing it can get tricky. But the following is valid Perl:

      %( = 0..9; print keys(%();
        I thought of that, but dismissed it. He could have named $( something different if he wanted to use parens for variables, or did "${var}" come later? I figured it had been in Perl from the start due to its parallel in bourne.
      Do you know why curlies, and not parens, are necessary here?

      Because parentheses only ever group things, never performing any nesting or scoping or accessing.

      No, I don't know why. Curlies are always used where are variables name is expected.
      my $x = ${$hash_ref}{key}; # Original syntax my $x = $hash_ref->{key}; # Newer, cleaner syntax print($foo, "bar\n"); print("${foo}bar\n"); # same print("$foobar\n"); # XXX my $x = ${"varname"}; # Symbolic reference

      etc.

      It strikes me that curlies (or braces) are used for legacy reasons given that the provenance of the language is the *NIX shell (see, to name but one, Perl).

      As we all know, *NIX shell uses braces in references to environment variables - when and only when the variable isn't followed by a printable, non-word boundary char can the braces be omitted e.g. ${foo}_bar is the var foo with _bar appended, whereas $foo_bar refers to a variable named foo_bar.

      This (string interpolation) is true for both shell and perl, but interestingly (I really ought to get out more:-) not in make(1) scripts - which can take braces or parens as separators.

      Update:

      Thanx to tilly for the hint.

      A user level that continues to overstate my experience :-))
        I don't understand what you mean by saying that Perl is like shell in this regard unless you are talking about string interpolation.