diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2009-02-03 11:54:13 -0800 |
---|---|---|
committer | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2009-02-03 11:54:13 -0800 |
commit | 4eb548a4dbc97795d3991c079853630d6914cab4 (patch) | |
tree | 56171e999ed65dc38fda5460c4a0fa3a58ce5378 /lib/Vyatta/Config.pm | |
parent | 300f2f918be3e807f628ad1200a72e389a59ecfe (diff) | |
download | vyatta-cfg-4eb548a4dbc97795d3991c079853630d6914cab4.tar.gz vyatta-cfg-4eb548a4dbc97795d3991c079853630d6914cab4.zip |
Cleanup confg library
Make this code pass perl critic. Mostly by changing it to return
proper booleans, and use three argument open.
Diffstat (limited to 'lib/Vyatta/Config.pm')
-rwxr-xr-x | lib/Vyatta/Config.pm | 91 |
1 files changed, 40 insertions, 51 deletions
diff --git a/lib/Vyatta/Config.pm b/lib/Vyatta/Config.pm index 9e57448..3f2191b 100755 --- a/lib/Vyatta/Config.pm +++ b/lib/Vyatta/Config.pm @@ -206,19 +206,16 @@ sub returnValue { $node =~ s/\//%2F/g; $node =~ s/\s+/\//g; - if ( -f "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node/node.val" ) { - open FILE, "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node/node.val" || return undef; - read FILE, $tmp, 16384; - close FILE; + open (my $file, '<', + "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node/node.val") + or return; # undefined - $tmp =~ s/\n$//; - return $tmp; - } - else { - return undef; - } -} + read $file, $tmp, 16384; + close $file; + $tmp =~ s/\n$//; + return $tmp; +} ## returnOrigValue("node") # returns the original value of "node" (i.e., before the current change; i.e., @@ -231,16 +228,14 @@ sub returnOrigValue { $node =~ s/\//%2F/g; $node =~ s/\s+/\//g; my $filepath = "$self->{_active_dir_base}$self->{_current_dir_level}/$node"; - if ( -f "$filepath/node.val") { - open FILE, "$filepath/node.val" || return undef; - read FILE, $tmp, 16384; - close FILE; - - $tmp =~ s/\n$//; - return $tmp; - } else { - return undef; - } + + open my $file, '<', $filepath + or return; # undef + read $file, $tmp, 16384; + close $file; + + $tmp =~ s/\n$//; + return $tmp; } ## returnValues("node") @@ -276,12 +271,7 @@ sub exists { $node =~ s/\//%2F/g; $node =~ s/\s+/\//g; - if ( -d "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node" ) { - #print "DEBUG: the dir is there\n"; - return !0; - } else { - return undef; - } + return ( -d "$self->{_new_config_dir_base}$self->{_current_dir_level}/$node" ); } ## existsOrig("node") @@ -291,11 +281,7 @@ sub existsOrig { $node =~ s/\//%2F/g; $node =~ s/\s+/\//g; - if ( -d "$self->{_active_dir_base}$self->{_current_dir_level}/$node" ) { - return 1; - } else { - return undef; - } + return ( -d "$self->{_active_dir_base}$self->{_current_dir_level}/$node" ); } ## isDeleted("node") @@ -478,34 +464,33 @@ sub getTmplPath { next; } # the path is not valid! - return undef; + return; } - return $tpath + return $tpath; } sub isTagNode { my $self = shift; my $cfg_path_ref = shift; my $tpath = $self->getTmplPath($cfg_path_ref); - return undef if (!defined($tpath)); - if (-d "$tpath/node.tag") { - return 1; - } - return 0; + return unless $tpath; # undef; + return (-d "$tpath/node.tag"); } sub hasTmplChildren { my $self = shift; my $cfg_path_ref = shift; my $tpath = $self->getTmplPath($cfg_path_ref); - return undef if (!defined($tpath)); - opendir(TDIR, $tpath) or return 0; - my @tchildren = grep !/^node\.def$/, (grep !/^\./, (readdir TDIR)); - closedir TDIR; - if (scalar(@tchildren) > 0) { - return 1; - } - return 0; + + return unless $tpath; # undefined + + opendir (my $tdir, $tpath) + or return; # false + + my @tchildren = grep !/^node\.def$/, (grep !/^\./, (readdir $tdir)); + closedir $tdir; + + return (scalar(@tchildren) > 0); } # $cfg_path_ref: ref to array containing the node path. @@ -516,12 +501,16 @@ sub parseTmpl { my $cfg_path_ref = shift; my ($is_multi, $is_text, $default) = (0, 0, undef); my $tpath = $self->getTmplPath($cfg_path_ref); - return undef if (!defined($tpath)); + + return unless $tpath; # undef if (! -r "$tpath/node.def") { return ($is_multi, $is_text); } - open(TMPL, "<$tpath/node.def") or return ($is_multi, $is_text); - foreach (<TMPL>) { + + open (my $tmp, '<', "$tpath/node.def") + or return ($is_multi, $is_text); + + foreach (<$tmp>) { if (/^multi:/) { $is_multi = 1; } @@ -532,7 +521,7 @@ sub parseTmpl { $default = $1; } } - close TMPL; + close $tmp; return ($is_multi, $is_text, $default); } |