Chinaunix首页 | 论坛 | 博客
  • 博客访问: 835852
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2012-03-27 10:39:00

from: ~lallip/perl/fall05/debugging.shtml
CGI::Dump

Dump is one of the functions exported in CGI.pm's :standard set. It's functionality is similar to that of Data::Dumper. Rather than pretty-printing a complex data structure, however, this module pretty-prints all of the parameters passed to your CGI script. That is to say that when called, it generates an HTML list of each parameter's name and value, so that you can see exactly what parameters were passed to your script. Don't forget that you must print the return value of this function - it doesn't do any printing on its own.


点击(此处)折叠或打开

  1. use CGI qw/:standard/;
  2. print Dump;

  3. perl test.pl test.txt

  4. <ul>
  5. <li><strong>keywords</strong></li>
  6. <ul>
  7. <li>test.txt</li>
  8. </ul>
  9. </ul>[
Benchmark

Analyzing two or more chunks of code to see how they compare time-wise is known as "Benchmarking". Perl provides a standard module that will Benchmark your code for you. It is named, unsurprisingly, Benchmark. Benchmark provides several helpful subroutines, but the most common is called cmpthese(). This subroutine takes two arguments: The number of iterations to run each method, and a hashref containing the code blocks (subroutines) you want to compare, keyed by a label for each block. It will run each subroutine the number of times specified, and then print out statistics telling you how they compare.

For example, to contained three different ways of creating a two dimensional array. Which one of these ways is "best"? Let's have Benchmark tell us:


点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Benchmark 'cmpthese';

  5. sub explicit {
  6.     my @two_d = ([ ('x') x 10 ],
  7.                  [ ('x') x 10 ],
  8.                  [ ('x') x 10 ],
  9.                  [ ('x') x 10 ],
  10.                  [ ('x') x 10 ]);
  11. }

  12. sub new_per_loop {
  13.     my @two_d;
  14.     for (0..4){
  15.         my @inner = ('x') x 10;
  16.         push @two_d, \@inner;
  17.     }
  18. }

  19. sub anon_ref_per_loop {
  20.     my @two_d;
  21.     for (0..4){
  22.         push @two_d, [ ('x') x 10 ];
  23.     }
  24. }

  25. sub nested {
  26.     my @two_d;
  27.     for my $i (0..4){
  28.         for my $j (0..9){
  29.             $two_d[$i][$j] = 'x';
  30.         }
  31.     }
  32. }
  33. cmpthese (100000, {
  34.                  'Explicit' => \&explicit,
  35.                  'New Array Per Loop' => \&new_per_loop,
  36.                  'Anon. Ref Per Loop' => \&anon_ref_per_loop,
  37.                  'Nested Loops' => \&nested,
  38.              }
  39.       );

                      Rate Nested Loops New Array Per Loop Anon. Ref Per Loop Explicit
Nested Loops       22472/s           --               -43%               -51%     -57%
New Array Per Loop 39216/s          75%                 --               -14%     -25%
Anon. Ref Per Loop 45455/s         102%                16%                 --     -13%
Explicit           52083/s         132%                33%                15%       --


Data::Dumper

The standard Data::Dumper module is very useful for examining exactly what is contained in your data structure (be it hash, array, or object (when we come to them) ). When you use this module, it exports one function, named Dumper. This function takes a reference to a data structure and returns a nicely formatted description of what that structure contains.


点击(此处)折叠或打开

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;

  5. my @foo = (5..10);
  6. #add one element to the end of the array
  7. #do you see the error?
  8. $foo[@foo+1] = 'last';

  9. print Dumper(\@foo);

  10. When run, this program shows you exactly what is inside @foo:

  11. $VAR1 = [
  12.           5,
  13.           6,
  14.           7,
  15.           8,
  16.           9,
  17.           10,
  18.           undef,
  19.           'last'
  20.         ];

__DATA__ &

Perl uses the __DATA__ marker as a pseudo-datafile. You can use this marker to write quick tests which would involve finding a file name, opening that file, and reading from that file. If you just want to test a piece of code that requires a file to be read (but don't want to test the actual file opening and reading), place the data that would be in the input file under the __DATA__ marker. You can then read from this pseudo-file using , without bothering to open an actual file:


点击(此处)折叠或打开

  1. #!/usr/bin/env perl
  2.     use strict;
  3.     use warnings;

  4.     while (my $line = <DATA>) {
  5.       chomp $line;
  6.       print "Size of line $.: ", length $line, "\n";
  7.     }

  8.     __DATA__
  9.     hello world
  10.     42
  11.     abcde

  12.     The above program would print:

  13.     Size of line 1: 11
  14.     Size of line 2: 2
  15.     Size of line 3: 5

  16. $.
The $. variable keeps track of the line numbers of the file currently being processed via a while (<$fh>) { ... } loop. More explicitly, it is the number of the last line read of the last file read.

__FILE__ & __LINE__

These are two special markers that return, respectively, the name of the file Perl is currently executing, and the Line number where it resides. These can be used in your own debugging statements, to remind yourself where your outputs were in the source code:

print "On line " . __LINE__ . " of file " . __FILE__ . ", \$foo = $foo\n";

Note that neither of these markers are variables, so they cannot be interpolated in a double-quoted string


 
阅读(871) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~