crazysniffable has asked for the wisdom of the Perl Monks concerning the following question:

An "easy" one for you all: what do you generally call
these constructs:
@{ } %{ }
As in:
foreach (sort keys %{ $hash_ref }) {
I know what they are, how to use them; I just don't know
they are called in order to discuss them to a student.

Grazie!

Replies are listed 'Best First'.
Re: Array/Hash References? Constructors?
by dsb (Chaplain) on Jul 21, 2001 at 01:49 UTC
    The way you are using them, that is what you use to dereference the scalar $hash_ref. The curly braces are optional.

    You could use:

    foreach (sort keys %$hash_ref) { print; }
    You use the appropriate symbol to what type of reference the scalar is holding. So...
    $foo = \$bar; print $$foo; $foo = \@bar; print for @ref; $foo = \%bar; print for sort keys %$bar;
    I would call them dereferencers.

    Amel - f.k.a. - kel

      hehe - for clarity I would make them mandatory :). Its only a personal thing but I have found that if you do the dereferencing in the more verbose form then you are always in no doubt what you are doing. Its also much harder to miss off the odd character as well.

      Again - its only a personal preference
Re: Array/Hash References? Constructors?
by bikeNomad (Priest) on Jul 21, 2001 at 05:42 UTC
    I don't know that these have catchy names. In perlref this is referred to as dereferencing a BLOCK that returns a reference. But I don't think I've ever heard anyone refer to these as such. From perlref:

    2. Anywhere you'd put an identifier (or chain of identifiers) as part + of a variable or subroutine name, you can replace the identifier with a +BLOCK returning a reference of the correct type. In other words, the pre +vious examples could be written like this: $bar = ${$scalarref}; push(@{$arrayref}, $filename); ${$arrayref}[0] = "January"; ${$hashref}{"KEY"} = "VALUE"; &{$coderef}(1,2,3); $globref->print("output\n"); # iff IO::Handle is loaded Admittedly, it's a little silly to use the curlies in this case, bu +t the BLOCK can contain any arbitrary expression, in particular, subscrip +ted expressions: &{ $dispatch{$index} }(1,2,3); # call correct routine Because of being able to omit the curlies for the simple case of "$ +$x", people often make the mistake of viewing the dereferencing symbols +as proper operators, and wonder about their precedence. If they were, though, you could use parentheses instead of braces. That's not th +e case. Consider the difference below; case 0 is a short-hand versio +n of case 1, not case 2: $$hashref{"KEY"} = "VALUE"; # CASE 0 ${$hashref}{"KEY"} = "VALUE"; # CASE 1 ${$hashref{"KEY"}} = "VALUE"; # CASE 2 ${$hashref->{"KEY"}} = "VALUE"; # CASE 3 Case 2 is also deceptive in that you're accessing a variable called %hashref, not dereferencing through $hashref to the hash it's presu +mably referencing. That would be case 3.
Re: Array/Hash References? Constructors?
by RhetTbull (Curate) on Jul 21, 2001 at 16:51 UTC
    Personally I prefer the highly technical term "thingy" as in "the array thingy" or "the hash thingy" ;-)
Re: Array/Hash References? Constructors?
by tadman (Prior) on Jul 21, 2001 at 10:29 UTC
    They are kind of like a C "cast", but only superficially, as they really don't allow you to convert between types. I would tend to call them "dereferences", which is more like C's pointer '*', only in this case you are specifying the type of thing that you are dereferencing explicitly. I would imagine that people who have not been exposed to C are more likely to be unfamiliar with pointers.

    Don't forget that there is also ${ } for scalar references, though this is less frequently used.

    The "spoken" version of Perl would be something like:
    my $x = \$z; # "$x is a reference to scalar $z" my $y = $$x; # "$y is a copy of the scalar referenced by $x" my $w = $row{b}; # "$w is assigned the value of %row's 'b' entry" my @v = @{$row{b}}; # "@v is a copy of the array referenced by %row's b entry"
    Note that when you assign something, you might have to further dereference it to make any use out of it. That is, $y might actually be another reference.
Re: Array/Hash References? Constructors?
by CharlesClarkson (Curate) on Jul 21, 2001 at 02:00 UTC

    In a quoted string they are called bare indentifiers. As in print "${bare}word;"


    HTH,
    Charles K. Clarkson