Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get variable name of subroutine input [duplicate]

Tags:

arrays

perl

Am passing one array and one scalar to a function to check where that value is part of array or not in case it is not part then push to array. For reference purpose what It has to display is while pushing it has to display the name of array . Here is my code

use v5.10.1;
use strict;
use warnings;

my @ARRAY1 = qw/This is array of backup /;
my @ARRAY2;


my $value = "version.xml" ;


sub CheckPush($$)
{

my $val = shift (@_);
my $array_ref= shift (@_);

     unless ($val ~~ @$array_ref )
     {
        print "$val is going to push to array  \n";
        push(@$array_ref,$val);
     }   
return (@$array_ref);
} 

@ARRAY1 = CheckPush($value,\@ARRAY1);
print "out \n";
foreach $_ (@ARRAY1) {
print "$_ \n";
}

@ARRAY2 = CheckPush ($value,\@ARRAY2);
print "out2 \n";
foreach $_ (@ARRAY2) {
print "$_ \n";
}

Out put should be like below

$val is going to push Array (@ARRAY1)
$val is going to push Array (@ARRAY2)
like image 268
Scg Avatar asked Nov 23 '22 02:11

Scg


1 Answers

Pass the name of the array along with the reference; it isn't that hard.

People do do things like this with source filters or PadWalker::var_name, but it isn't a good idea.

like image 56
ysth Avatar answered Jan 31 '23 20:01

ysth