Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to pass a hash and a string to a function, together in perl!

Tags:

perl

I am basically trying to pass a string and a hash to a subroutine in perl.

sub coru_excel {
    my(%pushed_hash, $filename) = @_;
    print Dumper(%pushed_hash);
}

But it seems data is getting mixed up. The dumped data also includes the $filename. here is the output.

...................
$VAR7 = 'Address';
$VAR8 = [
          '223 VIA DE
................
        ];
$VAR9 = 'data__a.xls'     <----- $filename
$VAR10 = undef;
$VAR11 = 'DBA';
$VAR12 = [
           'J & L iNC
..................
         ];

Here is how I called the subroutine.

coru_excel(%hash, "data_".$first."_".$last.".xls");
like image 653
Shubham Avatar asked Nov 28 '22 23:11

Shubham


1 Answers

Arguments are passed to subroutines as one undifferentiated list.

One solution is to reverse the order of the arguments so that the scalar is first.

sub coru_excel {
    my($filename, %pushed_hash) = @_;
}

coru_excel("FILE_NAME", %hash);

Another approach is to pass the hash by reference:

sub coru_excel {
    my($pushed_hash_ref, $filename) = @_;
}

coru_excel(\%hash, "FILE_NAME");
like image 89
FMc Avatar answered Dec 20 '22 01:12

FMc