zoukankan      html  css  js  c++  java
  • eval防止程序崩溃

    [root@redis01 sbin]# cat test.pl 
    use Redis;  
    use Sys::Hostname;
    use HTTP::Date qw(time2iso str2time time2iso time2isoz);  
    use Net::SMTP;
    sub basic() {
         $host = hostname;
         @lines=qx|/sbin/ifconfig|;
         @ip;
        print "-" x 80 ."
    ";
        foreach(@lines){
            if(/inet addr:([d.]+)/){
                push @ip,$1 unless $1 =~ /A127.0.0.1z/;
            }
        }
        print "${yellow}HOST: $host => IP: @ip$normal
    ";
        print "-" x 80 ."
    ";
    };
    &basic();
    sub send_mail{
    if (@_ != 2){print "error
    "};
        ($m,$n) = @_; 
        my $to_address  = $m;
    my $CurrTime = time2iso(time());
        my $to_address  = $m;
     my $mail_user   = 'zhaoyangjian@zjcap.cn';
        my $mail_pwd    = 'xxxxxx';
        my $mail_server = 'smtp.exmail.qq.com';
    
        my $from    = "From: $mail_user
    ";
        my $subject = "Subject: zjcap info
    ";
        my $info = "$CurrTime--$n";
         my $message = <<CONTENT; 
         $info
    CONTENT
        my $smtp = Net::SMTP->new($mail_server);
    
        $smtp->auth($mail_user, $mail_pwd) || die "Auth Error! $!";
        $smtp->mail($mail_user);
        $smtp->to($to_address);
    
        $smtp->data();             # begin the data
        $smtp->datasend($from);    # set user
        $smtp->datasend($subject); # set subject
        $smtp->datasend("
    
    ");
        $smtp->datasend("$message
    "); # set content
        $smtp->dataend();
        $smtp->quit();
    };
    $var;
    my $r = Redis->new( server => "127.0.0.1:6379",reconnect => 1,every=>60) or (die "can't connect to redis");  
    $r = Redis->new( password =>  'xx' );  
    $var=$r->get('test');
    print "$var is $var
    ";
    $r->quit; 
    print "qaxwscedv
    ";
    print "$var
    ";
    if ( defined($var) ){print "redis could connetct
    "}
    else
    {print "redis is lost",send_mail('zhaoyangjian@zjcap.cn',"@ip-redis is lost
    ")};
    
    
    
    [root@redis01 sbin]# perl test.pl 
    --------------------------------------------------------------------------------
    HOST: redis01 => IP: 10.51.35.244 120.26.137.80
    --------------------------------------------------------------------------------
    Redis server refused password at /usr/local/share/perl5/Redis.pm line 430
    	Redis::__ANON__('[auth] ERR invalid password,  at /usr/local/share/perl5/Redis...') called at /usr/local/share/perl5/Try/Tiny.pm line 104
    	Try::Tiny::try('CODE(0x1ced1e0)', 'Try::Tiny::Catch=REF(0x1fa9c40)') called at /usr/local/share/perl5/Redis.pm line 431
    	Redis::__build_sock('Redis=HASH(0x213d5e0)') called at /usr/local/share/perl5/Redis.pm line 402
    	Redis::__connect('Redis=HASH(0x213d5e0)') called at /usr/local/share/perl5/Redis.pm line 68
    	Redis::new('Redis', 'password', 'r,uLgt.313xx') called at test.pl line 51
    
    程序崩溃,没有往下面运行:
    
    加上eval后,
    eval{
    my $r = Redis->new( server => "127.0.0.1:6379",reconnect => 1,every=>60) or (die "can't connect to redis");  
    $r = Redis->new( password =>  'xx' );  
    $var=$r->get('test');
    print "$var is $var
    ";
    $r->quit; 
    };
    
    
    [root@redis01 sbin]# perl test.pl 
    --------------------------------------------------------------------------------
    HOST: redis01 => IP: 10.51.35.244 120.26.137.80
    --------------------------------------------------------------------------------
    qaxwscedv
    
    redis is lost1[root@redis01 sbin]# 
    

  • 相关阅读:
    20199118 2019-2020-2《网络攻防实践》第七周作业
    20199118 2019-2020-2 《网络攻防实践》第六周作业
    20199118 2019-2020-2 《网络攻防实践》第五周作业
    20199118《网络攻防实践》第四周作业
    C#调用webservice 不用默认配置文件 直接在构造函数配置地址
    SQL 字符串分隔函数
    查询Sqlserver数据库死锁的一个存储过程
    C# 守护进程 Windows服务 启动 exe
    SQLSERVER 存储过程中的事务
    EFCore连接Mysql DBFirst模式生成model
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351461.html
Copyright © 2011-2022 走看看