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

Greetings all. I have a coworker here that insists on accessing hash values like this:
$value = %hash->{key};
This apparently works but I have never seen something like that in the manuals or Perl books.
To test things out I tried this:
@array = ('a','b','c'); print @array->[2], "\n";

This works too and it seems to me that %hash and @array behave like references to themselves.

Would some wise monk care to shed some light on this?

Thanks!

Replies are listed 'Best First'.
Re: Unusual way of accessing hash values
by gaal (Parson) on May 05, 2005 at 12:04 UTC
    From perldiag:

    Using a hash as a reference is deprecated (D deprecated) You tried to use a hash as a reference, as in "%foo->{"bar"}" or "%$ref->{"hello"}". Versions of perl <= 5.6.1 used to allow this syntax, but shouldn’t have. It is now deprecate +d, and will be removed in a future version.
Re: Unusual way of accessing hash values
by gellyfish (Monsignor) on May 05, 2005 at 12:05 UTC

    In the hash example you would get:

    Using a hash as a reference is deprecated at ....
    in the array example you would get:
    Using an array as a reference is deprecated at ...
    if you had use warnings (or -w).

    /J\

    :
Re: Unusual way of accessing hash values
by polettix (Vicar) on May 05, 2005 at 12:42 UTC
    This seems to apply to function calls as well. As seen in holli's homenode, from the CB:
    diotalevi Huh. Foo->() is merely alternative syntax for Foo(). I never knew that.
    jZed a man walks into a Bar->(), bartender says, did you know that arrow is redundant?

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
Re: Unusual way of accessing hash values
by drone (Monk) on May 05, 2005 at 12:13 UTC
    Thanks gaal and gellyfish! Didn't even know there was a perldiag :)
    And of course using warnings would save me a lot of trouble in explaining some facts to my coworker!

      One other reason I sometimes temporarily turn on diagnostics is to find out what keyword to use when I want to selectively turn off a particular group of warnings. For example, in this case yo see that the body of the diagnostic message begins with (D deprecated). If for some reason I wanted to turn off such warnings in a suitably narrowed scope, I would do this:

      use strict; use warnings; # ... { no warnings 'deprecated'; %hash->{ silly } = 'foo'; }
      Granted, I can't think of any good reason for turning off this particular warning (except perhaps globally on a large body of legacy code), but there are more realistic examples. E.g.:
      use strict; use warnings; require Exporter; { no warnings 'once'; *import = \&Exporter::import; }
      The no warnings above silences a warning of the form
      Name "main::import" used only once: possible typo...

      Yes, one could silence this warning like this instead:

      *import = *import = \&Exporter::import;
      ...but I digress.

      the lowliest monk

      If you're ever unsure as to what a warning means, you can ...
      use diagnostics;
      which would print the exact text as posted in the the first reply.

        Or pipe the error messages through the splain program.

        Man, it'd be great if the cow-orker in question was named "Lucy". Lucy, joo gots some splain'in to do!