• 通过Net::Ping 理解Perl的面向对象


    <pre name="code" class="sql">v-dev-redis01:/root# cat a1.pl 
    sub check_dns{
     use  LWP::UserAgent;  
    use Net::Ping;
    $web = shift;
     $p = Net::Ping->new("icmp");
      if ($p->ping($web,2)){   
    return 1;
    }  
    else {  
    return 0;
    }} ;
    
    $var=&check_dns("$ARGV[0]"); 
    print "$var
    ";
    v-dev-redis01:/root# perl a1.pl  www.zjtest.com
    1
    
           If a default timeout ($def_timeout) in seconds is provided, it is used when a timeout is not given to the ping() method (below).  The timeout must be greater than 0 and the default, if
               not specified, is 5 seconds.
    
    
    jrhmpt01:/root# cat a3.pl 
    use Net::Ping;
    $p = Net::Ping->new("icmp");
    use Data::Dumper;  
     my $xx= Dumper($p);      
    print "111111111
    ";  
    print $xx;      
    print "
    "; 
    jrhmpt01:/root# perl a3.pl 
    111111111
    $VAR1 = bless( {
                     'proto_num' => 1,
                     'proto' => 'icmp',
                     'data' => '',
                     'device' => undef,
                     'tos' => undef,
                     'data_size' => 0,
                     'pid' => 30484,
                     'econnrefused' => undef,
                     'fh' => bless( *Symbol::GEN0, 'FileHandle' ),
                     'timeout' => 5,
                     'local_addr' => undef,
                     'seq' => 0,
                     'retrans' => '1.2'
                   }, 'Net::Ping' );
    
    
    生产$p 这个对象,
    sub new
    {
      my ($this,
          $proto,             # Optional protocol to use for pinging
          $timeout,           # Optional timeout in seconds
          $data_size,         # Optional additional bytes of data
          $device,            # Optional device to use
          $tos,               # Optional ToS to set
          ) = @_;
      my  $class = ref($this) || $this;
      my  $self = {};
      my ($cnt,               # Count through data bytes
          $min_datasize       # Minimum data bytes required
          );
    
      bless($self, $class);
    
    
    
    其中$class is Net::Ping
    
    $self 是$VAR1 = bless( {}, 'Net::Ping' );
    
    
    sub ping
    {
      my ($self,
          $host,              # Name or IP number of host to ping
          $timeout,           # Seconds after which ping times out
          ) = @_;
      my ($ip,                # Packed IP number of $host
          $ret,               # The return value
          $ping_time,         # When ping began
          );
    
    ping 方法的第一个参数是$self 对象
    
    
    第2个参数是  域名或者IP地址
    
    第三个参数是  超时秒数
    
    生产$p 这个对象,
    
    
      Functions
           Net::Ping->new([$proto [, $def_timeout [, $bytes [, $device [, $tos ]]]]]);
               Create a new ping object.  All of the parameters are optional.  $proto specifies the protocol to use when doing a ping.  The current choices are "tcp", "udp", "icmp", "stream", "syn",
               or "external".  The default is "tcp".
    
    
    创建一个new 的ping 对象
    


    
                                        
    
  • 相关阅读:
    USACO 1.2 Broken Necklace
    USACO 1.2 Friday the Thirteenth
    USACO 1.1 Greedy Gift Givers
    USACO 1.1 Your Ride Is Here
    CSP考试策略
    CF444A DZY Loves Physics【结论】
    树状数组-复习笔记
    CF792E Colored Balls【思维】
    USACO4.4 Shuttle Puzzle【bfs+优化】
    拓扑排序-学习笔记
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351086.html
Copyright © 2020-2023  润新知