发布新日志

  • Use Smart::Comments module for debugging

    2008-11-20 11:35:19

    use Smart::Comments;
     ### Expected: 2 * $prediction
     ###      Got: $result
    would prints:
     ### Expected: 42
     ###      Got: 13

    如果只是perl表达式,则:
    ### 2 * $prediction
    ### $result
    would prints:
    ### 2 * $prediction: 42
    ### $result: 13

    调试完毕后,不需要移除这些注释行,只需要注释掉第一行即可。

    另外这个模块还提供了processing bar功能:
    #### Debugging here...

        for (@values) {         ### Progress: 0...  100
            do_stuff();
        }

    Click the url below for more details:
    http://search.cpan.org/dist/Smart-Comments-v1.0.3/lib/Smart/Comments.pm

  • 将不可见的符号变得可见

    2008-11-20 11:01:30

    在debug时,如果只是试验,最简单的调试方法就是到处添加一些print()语句。
    但是这种方式的弊病是,不容易发现一些特殊的字符输出,比如\n\r\t

    范例:
    一个常见的错误是当从外部资源读入数据时,忘记使用chomp()。
    while (<$file_list>){
      warn "Processing $_";
      next unless -e $_;
      &process_file($_);
    }
    输出中看不出什么问题,但是&process_file sub 却不会执行。
    改变调试行,让文件名更加明显:
    while (<$file_list>){
      warn "Processing '$_'";
      next unless -e $_;
      &process_file($_);
    }
    我们将发现所有的文件名末尾都有换行符,fix it:
    while (<$file_list>){
      chomp;
      next unless -e $_;
      &process_file($_);
    }

  • 快速定位编译错误

    2008-11-20 10:49:29

    1、使用 -wc 编译:(perl不会运行此程序)
    $perl -wc what_went_wrong.pl

    2、中分法判断出错代码:
    在中间行行头插入 __END__ 标记,再次运行perl -wc。如果出错,则错误在文件前一半,否则后一半。
    (标记后的代码都会被转为数据,从而使得Perl忽略它们。)

  • Use the Net::FTP module

    2008-11-12 14:16:33

    use Net::FTP;

    $ftp = Net::FTP->new("some.host.name", Debug => 0)
      or die "Cannot connect to some.host.name: $@";

    $ftp->login("anonymous",'-anonymous@')
      or die "Cannot login ", $ftp->message;

    $ftp->cwd("/pub")
      or die "Cannot change working directory ", $ftp->message;

    $ftp->get("that.file")
      or die "get failed ", $ftp->message;

    $ftp->quit;

  • Use the Net::Telnet module

    2008-11-12 14:12:03

    use warnings;
    use Net::Telnet ();
    my ($hostname, $username, $passwd);
    $hostname = "192.168.120.48";
    $username = "cnshroot";
    $passwd = "shroot";
    $telnet = new Net::Telnet (Timeout => 10,
                          Prompt => '/-bash-3\.00# $/');
    $telnet->open($hostname);
    $telnet->login($username, $passwd);
    @lines = $telnet->cmd("who");
    print @lines;
    $telnet->close;
    exit;
  • Use perl to simulate tail command

    2008-11-12 14:08:35

    $logfile = "installation.log";
    $endOfFile = "Log_is_the_end_of_file";
    $installfile = "interactivelyInstallPanINM.pl";

    ## Clean up log file
    open CLEAN, "> $logfile"
         or die "Can't open the file: $logfile! ($!)";
    close CLEAN;

    ## Call $installfile to write log into the log file
    open FH, "interactivelyInstallPanINM.pl |" or die "fork: $!";

    ## Simulate the tail command.
    open FILE, "< $logfile"
         or die "Can't open the file: $logfile! ($!)";

    ## read until end of file
    while (1) {
        my $_ = <FILE>;
        if (/\b$endOfFile\b/){
          last;
        }else{
         print;
        }
    }
    ## last will goto here
    close FILE;
    close FH;

  • Use the Net::SMTP module to send an email through an ms exchange smtp server

    2008-11-12 14:02:02

    use Net::SMTP;

    my $smtpSer = 'mailhost';
    my $from = 'from@xxx.com';
    my $to = 'to@xxx.com';
    my $subject = 'Test';
    my $body = "A simple test message";

    $smtp = Net::SMTP->new($smtpSer);

    $smtp->mail($ENV{USER});
    $smtp->to($to);

    $smtp->data();
    $smtp->datasend("From: $from\n");
    $smtp->datasend("To: $to\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("Body: $body\n");
    $smtp->dataend();

    $smtp->quit;

    ##############################################################################

    perldoc Net::SMTP for more details

    If you need authentication (ie. server does not relay) then substitute Net::SMTP for Net::SMTP_auth. The trick to make this two work together is to create a email message with MIME::Lite (you can add attachments, use html or text) then convert the MIME::Lite object into a string (there is a method to do it within the module) then using the datasend(string) method from Net::SMTP your email will be ready to go.

  • 返回元素在数组中的索引值

    2008-11-12 11:02:21

    use warnings;
    sub whichIs{
     my ($str, @list) = @_;
     foreach (0..$#list){
      print $list[$_], "\n";
      if ($str eq $list[$_]){
       return $_;
      } 
     }
     -1;
    }
    my @names = qw / a b c fda wer fda fwefwkjl xxx dfao/;
    my $wanna = "xxx";
    print "\$wanna - $wanna is the ", &whichIs($wanna, @names), " of @names.\n";

    #############################################################################
    C:\Perl\learn>"which is.pl"
    a
    b
    c
    fda
    wer
    fda
    fwefwkjl
    xxx
    $wanna - xxx is the 7 of a b c fda wer fda fwefwkjl xxx dfao.

  • printf 右对齐打印

    2008-11-12 10:55:30

    my @items = qw / wilma dino pebbles /;
    printf "The items are: \n".("%10s\n" x @items), @items;

    ########################################################
    C:\Perl\learn>printf.pl
    The items are:
         wilma
          dino
       pebbles

  • high water mark 算法(返回最大值)

    2008-11-12 10:46:47

    use warnings;
    sub max{
     my $num;
     foreach (@_){
      if ($num < $_){
       #print $num, "\n";
       $num = $_;
      }
     }
     $num;
    }
    @numbers = qw /342 6436 43 1 -13 55555 431/;
    print "The max number is ", &max(@numbers), ".\n";

    ###################################################
    Print log is as below:

    C:\Perl\learn>"high-water mark.pl"
    Use of uninitialized value $num in numeric lt (<) at C:\Perl\learn\high-water ma
    rk.pl line 5.
    The max number is 55555.

  • Get test cases' title

    2008-11-12 10:33:44

    测试工作中文本处理是一项非常繁琐的事情,利用Perl可以事半功倍。下面的代码将从杂乱的文本文件tcs.TXT中提取测试用例名称,并保存到testcase.txt。详见附件。

    use warnings;
    my $filename = "tcs.TXT";
    my $exportFile = "testcase.txt";
    open FILE, $filename
     or die "Can't open $filename: $!";
    open TC, ">$exportFile"
     or die "Can't create $exportFile: $!";

    while(<FILE>){
     chomp;
     if (/TEST CASE: (.*)     /){
      chomp ($1);
      print TC $1;
     }
     if (/DEVICE UNDER TEST: (.*)\s+/){
      chomp ($1);
      print TC "_$1\n";
     }
    }

  • export exceptions

    2008-11-12 10:14:04

    我们经常需要浏览大量的日志文件,so,将exception or error抓出来是非常有用的。下面的代码将读取服务器上的javlin.log文件,并将含有exception or error关键字的行保存到本地的Err.log文件里。

    use warnings;
    my $file = '\\\\192.168.127.143\JLogger\javlin.log';
    open JLOG, $file
     or die "Can't open $file! ($!)";
    if (open ERRLOG, ">Err.log"){
     while (<JLOG>){
      if(/exception|error/i){
       print ERRLOG $_;
      }  
     }
     close ERRLOG;
    } else {
     die "Can't create Err.log: $!"; 
    }
    close JLOG;

  • Hello Perl

    2008-11-12 09:40:08

    print "Hello Perl!\n";
Open Toolbar