in reply to Re: Nested Hash Dereferencing Syntax
in thread Nested Hash Dereferencing Syntax

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!

Replies are listed 'Best First'.
Re^3: Nested Hash Dereferencing Syntax
by tilly (Archbishop) on Feb 24, 2009 at 18:19 UTC
    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.
        I agree that ${...} is certainly originally in Perl because of the familiarity of the syntax in shell. But Perl is willing to change syntax if a new one is clearer - witness the switch from ' to :: as a package separator.

        But my understanding is that references and most of our dereferencing syntax was added in Perl 5. Which means that backwards compatibility would prevent the $(...) syntax from being introduced after that.

Re^3: Nested Hash Dereferencing Syntax
by chromatic (Archbishop) on Feb 24, 2009 at 19:03 UTC
    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.

Re^3: Nested Hash Dereferencing Syntax
by ikegami (Patriarch) on Feb 24, 2009 at 17:38 UTC
    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.

Re^3: Nested Hash Dereferencing Syntax
by Bloodnok (Vicar) on Feb 24, 2009 at 18:11 UTC
    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.