Commit 525f59e1 by PotatoGim

Perl 연관 배열을 이용한 설정 관리 일부 코드 추가

parent c39204ed
#!/usr/bin/perl
#=============================================================================
# FILE: configurator.pl
# USAGE: ./configurator.pl
# BRIEF:
# OPTIONS: ---
# AUTHOR: Ji-Hyeon Gim <potatogim@gluesys.com>
# ORGANIZATION: Network Storage Team
# DATE: 03/03/2015 05:15:06 PM
# VERSION: 1.0
# LICENSE: BSD 3-Clause License
# NOTE: ----
#=============================================================================
use strict;
use warnings;
use utf8;
sub set_config
{
my $self = shift;
my %args = @_;
if (!defined($args{key}) || !length($args{key}))
{
warn "[ERR] Invalid parameter: key";
return -1;
}
my $table = $self->table;
for (my @q = ({path => "", val => \$table})
; @q
; )
{
my $node = shift(@q);
if ($node->{path} eq $args{key})
{
${$node->{val}} = $args{value};
last;
}
elsif (ref(${$node->{val}}) eq "HASH")
{
push (@q, map {
{
path => length($node->{path})
? $node->{path} . "." . $_
: $_,
val => \${$node->{val}}->{$_},
};
} keys(%{${$node->{val}}}));
}
}
return 0;
}
sub get_config
{
my $self = shift;
my %args = @_;
if (!defined($args{key}) || !length($args{key}))
{
warn "[ERR] Invalid parameter: key";
return undef;
}
my $table = $self->table;
my $value = undef;
for (my @q = ({path => "", val => $table})
; @q
; )
{
my $node = shift(@q);
if ($node->{path} eq $args{key})
{
$value = $node->{val};
last;
}
elsif (ref($node->{val}) eq "HASH")
{
push (@q, map {
{
path => length($node->{path})
? $node->{path} . "." . $_
: $_,
val => $node->{val}->{$_},
};
} keys(%{$node->{val}}));
}
}
return $value;
}
1;
__END__
=encoding utf8
=head1 NAME
configurator.pl
=head1 SYNOPSIS
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@gluesys.com>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
=head1 SEE ALSO
=cut
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment