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

Why are the braces around $_[0] in function STORE?
package Cents; sub TIESCALAR { bless \my $self, shift } sub STORE { ${$_[0]} = $_[1] } sub FETCH { sprintf "%.02f", ${ my $self = shift} } package main; tie $bucks, "Cents"; $bucks = 45.00; print "$bucks\n"; $bucks *= 1.0715; print "$bucks\n";

Replies are listed 'Best First'.
Re: what does the brace do
by ikegami (Patriarch) on Nov 05, 2007 at 20:16 UTC

    ${$var} means
    "the scalar variable referenced by the reference in $var." or
    "the scalar package variable referenced by the name in $var."

    my $some_scalar = 'abc'; my $ref = \$some_scalar; print $ref, "\n"; # SCALAR(0x225e9c) print ${$ref}, "\n"; # abc
    $some_scalar = 'abc'; my $name = 'some_scalar'; print $name, "\n"; # some_scalar print ${$name}, "\n"; # abc

    See perlref for the former, and Why it's stupid to "use a variable as a variable name" for the latter.

Re: what does the brace do
by gamache (Friar) on Nov 05, 2007 at 20:12 UTC
    Braces are most often used to allow correct interpolation in strings, e.g. print "${hours}h ${minutes}m", and to dereference things which are more than just variable names, as in your example. Here, you're treating $_[0] (the first arg to the subroutine) as a scalar reference, dereferencing it, and setting its value to be that of the second subroutine argument. See perlref.
Re: what does the brace do
by Fletch (Bishop) on Nov 05, 2007 at 20:30 UTC