KW Perl Mongers Perl Modules: A Look Under the Hood #7

Arrow Operator

How do you get back the elements of an array through a reference?

                 $a[0] = 1;
      ${$array_ref}[0] = 1; # same thing

That looks messy. Fortunately, there's a simplification.

      $array_ref->[0] = 1;  # same thing. note square brackets.
      $hash_ref->{foo} = 1; # similarly for hash. note curly brackets.
      $sub_ref->(@args);    # similarly for subroutines. note parens.
      $object_ref->method(@args); # ...and similarly, for object and method.
  • The last one, for methods, adds a bit of extra magic...

  • ... it passes the class of the method as the first parameter.

<< Previous | Index | Next >> Copyright © 2004 Daniel Allen