Friday, October 3, 2008

perl: another way to dereference

When you have structures that have nested arrays or hashes which you want to dereference on the fly w/o using extra variable to store reference you can use special syntax:

%{reference_to_hash} and @{reference_to_array}
Next piece of code shows the common usage
$struct = [1, 2, 3, {a=>1, b=>2, c=>[1,2]}];

%hash = %{$struct->[3]};

@array = @{$struct->[3]->{c}};
This is useful when you want to work with structures but not with their references
push @{$struct->[3]->{c}}, (3, 4);

No comments: