perl 检验ip地址是否合法

根据合法ip规格,写了个检验程序:

合法ip:(1..255).(0..255).(0..255).(0..255)范畴

sub report{
    print $_[0];
    exit($_[1]);
}
sub check_ip{
if ($_[0] =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/){
    if($1>0&&$1<256){
        if($2>-1&&$2<256){
            if($3>-1&&$3<256){
                if($4>-1&&$4<256){
                    &report(qq($_[0] address True!\n),0);
                }
                else{
                    &report(qq(ip[4] need ">-1 <256"\n),1);
                } 
        
              }
              else{
                   &report(qq(ip[3] need ">-1 <256"\n),1);
              }
        
          }
          else{
              &report(qq(ip[2] need ">-1 <256"\n),1);;
          }
      }
    else{
        &report(qq(ip[1] need ">0 <256"\n),1);
    }
}
else{
   &report(qq($_[0] address Error!\n),1);
}
}
&check_ip($ARGV[0]);

 

有错误的地方,请指出。谢谢!

你可能感兴趣的:(IP,职场,perl,休闲)