Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference found where even-sized list expected

Tags:

perl

I am writing this code in perl where i create a unique key and then assign a value to it.

  sub populate {
      my $file = shift;
      my %HoH = shift;

      open(INFILE,$file);
      .
      .
      .  
      $final_name = $prepend.$five;
      $HoH{$final_name} = $seven;
 }

Now i am passing in two parameters to a subroutine which id like

&populate(\%abc,$file_1);
&populate(\%xyz,$file_2);

Why does it give me an error like this:

Reference found where even-sized list expected
like image 463
kunal Avatar asked Dec 12 '22 10:12

kunal


1 Answers

Because your hash is assigned to a reference, and not a hash (even-sized list). You need to do:

my $hashref = shift;

...

$hashref->{$final_name} = $seven;

ETA: You should call subroutines without &, e.g. populate(...), unless you specifically want to override the prototype of the sub. If you don't know what a prototype is, just don't use &.

ETA2: You really should use a lexical filehandle and three-argument open. Consider this scenario:

open INFILE, $file;
some_sub();
$args = <INFILE>;  # <--- Now reading from a closed filehandle

sub some_sub {
    open INFILE, $some_file;
    random code...
    close INFILE;
}
like image 162
TLP Avatar answered Jan 02 '23 11:01

TLP