Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing hash reference in perl

The following Perl code prints Value:0. Is there a way to fix it other than by adding a dummy key to the hash before hash reference is passed to the subroutine ?

#!/usr/bin/perl 
use warnings;
use strict;

my $Hash;

#$Hash->{Key1} = 1234;

Init($Hash);

printf("Value:%d\n",$Hash->{Key});

sub Init
{
    my ($Hash) = @_;
    $Hash->{Key}=10;
}
like image 261
Jean Avatar asked Mar 23 '23 13:03

Jean


1 Answers

Initialize an empty hash reference.

#!/usr/bin/perl 
use warnings;
use strict;

my $Hash = {};

Init($Hash);

printf("Value:%d\n",$Hash->{Key});

sub Init
{
    my ($Hash) = @_;
    $Hash->{Key}=10;
}
like image 139
Jack Bracken Avatar answered Mar 27 '23 19:03

Jack Bracken