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.
1. Modify the passed parameters in a function: If a function receives a reference to a variable, it can modify the value of the variable. For example, the following program variables are swapped using references.
A reference is a name constant for an address. You need to initialize the reference during declaration. Once a reference is established to a variable, you cannot change the reference to reference another variable.
Correct Option: B. const is a keyword constant in C program.
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'
];
function([ keys %h1 ], [ keys %h2 ]);
From perldoc perlref
:
A reference to an anonymous array can be created using square brackets:
$arrayref = [1, 2, ['a', 'b', 'c']];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With