Tuesday, April 07, 2009

perl - hash, array, reference

About hash, array, reference

solution 1,

#!/usr/bin/perl -w

use strict;

my @array;
foreach (0..9) {
handler(\@array);
}
for my $i (0..$#array) {
print $array[$i]->{key},"\n";
}

sub handler {
my ($aryhref)=@_;
my (%hash);
$hash{key}="abc";
push @$aryhref, \%hash;
}

=========================
solution 2,

#!/usr/bin/perl -w

use strict;

my @array;

my %hash;
foreach (0..9) {
ca(\%hash);
push @array, \%hash;
}
for my $i (0..$#array) {
print $array[$i]->{username},"\n";
}

sub ca {
my ($hashref)=@_;
$hashref->{username}="abc";
}

use strict;
%{hashref}->{key}
got "Using a hash as a reference is deprecated"
suggest ${hashref}{key} instead.

about Perl Module installation

No comments: