Search code examples
perlvariablessubroutinedereference

Perl : Dereference between @_ and $_


I have a question with the following Code:

#!/usr/bin/perl

use strict;
use warnings;

my %dmax=("dad" => "aaa","asd" => "bbb");
my %dmin=("dad" => "ccc","asd" => "ddd");

&foreach_schleife(\%dmax,\%dmin);

sub foreach_schleife {
        my $concat;
        my $i=0;

        foreach my $keys (sort keys %{$_[0]}) {
                while ($_[$i]) {
                        $concat.="$_[$i]{$keys} ";
                        print $_[$i]{$keys}."\n";
                        $i++;
                }
                $i=0;
                $concat="";
        }
}

The Output is:

   bbb
   ddd
   aaa
   ccc

I don't understand this. Normally you must dereference references on hashes,arrays etc. Why not here? Its enough to write :

$_[$i]{$keys}."\n";

and not something like that:

$$_[$i]{$keys}."\n";

Why? Has it something to do with the speciality of the variable @_/$_?


Solution

  • @_ is the array of subroutine arguments, hence $_[$index] accesses the element at $index

    Dereferencing is only good if you have references, but @_ isn't one.