Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 483
Sandra Schlichting Avatar asked Apr 08 '11 09:04

Sandra Schlichting


People also ask

Can you change value of reference variable?

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.

Can we change reference variable in C++?

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.

Which of the following is used to prevent changing the reference of a variable?

Correct Option: B. const is a keyword constant in C program.


2 Answers

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'
        ];
like image 165
Nikhil Jain Avatar answered Oct 15 '22 10:10

Nikhil Jain


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']];

like image 6
Eugene Yarmash Avatar answered Oct 15 '22 11:10

Eugene Yarmash