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

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Reaped: Why do you need $ when accessing array and hash elements in Perl?

Replies are listed 'Best First'.
Re: Why do you need $ when accessing array and hash elements in Perl?
by ww (Archbishop) on Apr 20, 2009 at 12:52 UTC
      plagIarism
Re: Why do you need $ when accessing array and hash elements in Perl?
by ELISHEVA (Prior) on Apr 20, 2009 at 14:39 UTC
      Addendum to ELESHEVA's parent node:
      You dishonor them by not giving them credit for the question and presenting it as your own and you dishonor yourself.
Re: Why do you need $ when accessing array and hash elements in Perl?
by jlk (Hermit) on Apr 20, 2009 at 12:45 UTC
    Because that is the way that you access the arrays and hashes in Perl, that you have created. Granted, that will all change in Perl 6, but in Perl 5 and below, that is how it is done.

    Regards,

    Jeff

    "Every time Linux boots, a penguin gets its wings"

Re: Why do you need $ when accessing array and hash elements in Perl?
by lostjimmy (Chaplain) on Apr 20, 2009 at 13:51 UTC
    Wouldn't it be illegal Perl code if it was anything but a $ in that place?

    The reason the sigil is required is that it designates what you are retrieving from the container. So for instance, @array[] is suggesting that you are accessing an array and expecting an array from it. This is called an array slice. Similarly, @hash{} is a hash slice, and you are expecting more than one value from the hash. To retrieve multiple values, you have to provide multiple indexes/keys. So for an array: my @values = @array[1, 3, 5, 7], and for a hash: my @values = @hash{qw/a b c/};

Re: Why do you need $ when accessing array and hash elements in Perl?
by cdarke (Prior) on Apr 20, 2009 at 14:12 UTC
    The $ indicates the context of what follows. The general point about sigils is an interesting one. Comparing Perl with Python (which does not have sigils on variables, but does use them elsewhere), Python does not support interpolation, so variables cannot be embedded inside double "quotes". It also has to jump through hoops to achieve things like @hash{@keys} = @values. This is not to flame Python, those guys would say the price of sigils is too high.

    Perl is Open Source - there is nothing stopping you from downloading the source and altering it to do whatever you like.
Re: Why do you need $ when accessing array and hash elements in Perl?
by Anonymous Monk on Apr 20, 2009 at 12:39 UTC