New hash/hashref arguments handling function.

This commit is contained in:
Chris 2012-08-09 12:28:39 +01:00
parent 6f622105dc
commit 635d7182b4

View File

@ -31,7 +31,7 @@ use strict;
our @ISA = qw(Exporter);
our @EXPORT = qw();
our @EXPORT_OK = qw(path_join resolve_path check_directory load_file save_file superchomp trimspace lead_zero string_in_array blind_untaint title_case sentence_case is_defined_numeric rfc822_date get_proc_size find_bin untaint_path read_pid write_pid);
our @EXPORT_OK = qw(path_join resolve_path check_directory load_file save_file superchomp trimspace lead_zero string_in_array blind_untaint title_case sentence_case is_defined_numeric rfc822_date get_proc_size find_bin untaint_path read_pid write_pid hash_or_hashref);
# ============================================================================
@ -385,6 +385,33 @@ sub rfc822_date {
}
## @method $ hash_or_hashref(@args)
# Given a list of arguments, if the first argument is a hashref it is returned,
# otherwise if the list length is nonzero and even, the arguments are shoved
# into a hash and a reference to that is returned. If the argument list is
# empty or its length is odd, and empty hashref is returned.
#
# @param args A list of arguments, may either be a hashref or a list of key/value
# pairs to place into a hash.
# @return A hashref.
sub hash_or_hashref {
my $len = scalar(@_);
return {} unless($len);
# Even number of args? Shove them into a hash and get a ref
if($len % 2 == 0) {
return { @_ };
# First arg is a hashref? Return it
} elsif(ref($_[0]) eq "HASH") {
return $_[0];
}
# No idea what to do, so give up.
return {};
}
# ============================================================================
# OS specific functions