这是一个稍微更易读的混沌答案版本:
# Set sane variable names... my ($is_required, $default, $pattern) = @_ # Convert the external string convention for simpler evaluation... $default = undef if $default eq 'NULL' # Refuse problematic input before going any further... if ($input ne '' && $input !~ $pattern || $input =~ /'.+'/) { print "! Format not respected. Match : /$pattern/ (without \' \')\n"; next; } # If there was no input string... if($input eq '') { # Set the default, if one was provided... if( $default ) { print "+ Using default value\n"; $input = $default; } # otherwise, complain if a default was required... else { if( $is_required eq 'y' ){ print "! Mandatory parameter not filled in\n"; next; } print "+ Ignoring parameter (no input or default provided).\n"; } }
关键点是:
你不需要别人,如果你是退出当前环下一个
应首先处理特殊情况
使用命名变量可以极大地提高可读性