次のコードを前提として、ハッシュ参照の配列を反復処理する方法を理解しようとしています(または、少なくともハッシュ参照の配列だと思います)。つまり、$Policy->rules()
?
おそらく、構造の配列を作成するためのより良い方法があるでしょう、私は提案を受け入れています。
use Class::Struct;
use Data::Dumper;
struct Policy => {
listings => '@', # Will treat like rules eventually.
rules => '@', # an array of rules
};
struct Rule => {
direction => '$',
id => '$',
};
$policy = Policy->new();
$rule1 = Rule->new();
$rule1->direction('Any');
$rule1->id(1);
$rule2 = Rule->new();
$rule2->direction('Inbound');
$rule2->id(2);
$rule3 = Rule->new();
$rule3->direction('Outbound');
$rule3->id(3);
push($policy->rules(),$rule1);
push($policy->rules(),$rule2);
push($policy->rules(),$rule3);
$Data::Dumper::Indent = $Data::Dumper::Terse = 1;
print Dumper \$policy;
出力:
\bless( {
'Policy::listings' => [],
'Policy::rules' => [
bless( {
'Rule::id' => 1,
'Rule::direction' => 'Any'
}, 'Rule' ),
bless( {
'Rule::id' => 2,
'Rule::direction' => 'Inbound'
}, 'Rule' ),
bless( {
'Rule::id' => 3,
'Rule::direction' => 'Outbound'
}, 'Rule' )
]
}, 'Policy' )