Search code examples
arraysperlhashreferenceperl-data-structures

Can references be made without declaring a variable first?


I have this code that works

my @new = keys %h1;
my @old = keys %h2;

function(\@new, \@old);

but can it be done without having to declare variables first?

function must have its arguments as references.


Solution

  • use strict;
    use Data::Dumper;
    
    my %test = (key1 => "value",key2 => "value2");
    my %test2 = (key3 => "value3",key4 => "value4");
    
    test_sub([keys %test], [keys %test2]);
    
    sub test_sub{
     my $ref_arr = shift;
     my $ref_arr2 = shift;
     print Dumper($ref_arr);
     print Dumper($ref_arr2);
    }
    

    Output:

    $VAR1 = [
              'key2',
              'key1'
            ];
    $VAR1 = [
              'key4',
              'key3'
            ];