Perl Tips(1)

一 Perl的数据库操作及错误捕捉机制
    Perl可以使用eval{};/if($@){}来捕捉错误.与c++和java的try/catch类似。
    这里着重介绍一下数据库编程下的错误捕捉。
    我这里使用的是DBI模块,数据库是MySQL。
  1  首先是数据库连接模块。
my $database_name = "test";
my $database_host = "192.168.0.1";
my $database ="dbi:mysql:dbname=$database_name;host=$database_host";
my $db_user = "root";
my $db_password = "root";
sub connectDB
 {
            my $database ="dbi:mysql:dbname=$db_name;host=$db_host".":"."$db_port";
            $dbh = DBI->connect($database,$db_user,$db_pswd,{RaiseError =>1,AutoCommit =>0,PrintError =>0}) or die "can't connect!: $DBI::errstr";
}

说明:
RaiseError =>1 用途:在DBI产生错误是抛出例外,使用eval时必须打开这个控制,打开错误产生,1为打开,0为关闭,
AutoCommit =>0 用途,自动事务处理。1为打开,0为关闭。
PrintError==>0 用途:DBI用警告方式显示错误信息,1为打开,0为关闭。
$DBI::errstr 为错误代码。

2  数据库操作部分:
         eval{
                $sth = $dbh->prepare( "SELECT  *  FROM test ;");
                my $rc = $sth->execute;
                while(@record_db_part= $sth->fetchrow_array){
                        #处理操作;
                    }
            };
            if($@) {
                $errhandel=substr($@,30,16);
                $err_connect=" Lost connection";
                if ($errhandel eq $err_connect) {
                    #错误处理a;
                }
                else{
                    #错误处理b;
                }
                die;
            }
            $sth->finish;
            $dbh->disconnect;
说明:
1 所有数据库操作部分写在eval{}块中。
2 eval抛出的错误存在$@中,错误处理写在if($@){}块中。
3 $sth为 statement handle,用来存储SELECT的结果
4
$dbh为database handle,用来操作数据库。
5 $sth->fetchrow_array 返回结果为数组。还有多种返回值可以选择。
    $sth->bind_columns(undef, /$fields1, /$fields2, /$fields3, /$fields4); 绑定字段
6 $dbh->prepare( 'SELECT * FROM test;'); 所有select型语句用prepare();CREATE,DELETE用do();
7 $sth->finish;最好加上,否则有时会报错:DBI::db=HASH(0x812938)->disconnect invalidates 1 active statement handle
(either destroy statement handles or call finish on them before disconnecting)
at ./pdfshorten.pl line 37.
8 其他常用属性。
    $rows = $sth->rows; 返回查询后的记录数。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"This book was a joy to read. It covered all sorts of techniques for debugging, including 'defensive' paradigms that will eliminate bugs in the first place. As coach of the USA Programming Team, I find the most difficult thing to teach is debugging. This is the first text I've even heard of that attacks the problem. It does a fine job. Please encourage these guys to write more." -Rob Kolstad Perl Debugged provides the expertise and solutions developers require for coding better, faster, and more reliably in Perl. Focusing on debugging, the most vexing aspect of programming in Perl, this example-rich reference and how-to guide minimizes development, troubleshooting, and maintenance time resulting in the creation of elegant and error-free Perl code. Designed for the novice to intermediate software developer, Perl Debugged will save the programmer time and frustration in debugging Perl programs. Based on the authors' extensive experience with the language, this book guides developers through the entire programming process, tackling the benefits, plights, and pitfalls of Perl programming. Beginning with a guided tour of the Perl documentation, the book progresses to debugging, testing, and performance issues, and also devotes a chapter to CGI programming in Perl. Throughout the book, the authors espouse defensible paradigms for improving the accuracy and performance of Perl code. In addition, Perl Debugged includes Scott and Wright's "Perls of Wisdom" which summarize key ideas from each of the chapters, and an appendix containing a comprehensive listing of Perl debugger commands. In this exceptional reference and debugging guide, the authors cover every aspect of efficient Perl programming, including: · CGI programming-special tips for debugging this type of Perl program · How to develop the proper mindset for developing and programming effectively in Perl · Perl "gotchas"-how to understand them, work around them, and avoid them · "Antibugging"-the authors' rules of thumb on how to code defensively · The Perl debugger -the authors' guide to using this Perl built-in Common syntax errors and how to track down their causes · Semantical errors-why code may appear correct but the programs do not work · How to improve the performance of resource-hungry programs · Tips and advice for programmers moving to Perl from other language environments Focusing on the process of Perl programming and guidelines for identifying and correcting mistakes, Perl Debugged helps the developer to write better Perl programs immediately and become better programmers in general.
Table of Content Table of Content.........................................................................................................................i Copyright....................................................................................................................................v Dedication.............................................................................................................................vi Preface......................................................................................................................................vi Perlness................................................................................................................................vi Who Are You?.....................................................................................................................vii What This Book Covers.....................................................................................................vii Getting Perl.........................................................................................................................viii Typographical Conventions...............................................................................................ix For Further Reference........................................................................................................ix Perl Versions.........................................................................................................................x Acknowledgments................................................................................................................x Chapter 1. Introduction.............................................................................................................1 1.1 Reality..............................................................................................................................1 1.2 Why Perl?........................................................................................................................2 1.3 Know the Environment..................................................................................................3 1.4 Know the Language.......................................................................................................3 1.5 Online Documentation...................................................................................................4 1.6 References......................................................................................................................9 Chapter 2. The Zen of Perl Developing...............................................................................10 2.1 Attitudes.........................................................................................................................11 2.2 Beliefs............................................................................................................................12 2.3 Behavior........................................................................................................................13 2.4 Improve Your Craft......................................................................................................14 2.5 The Bottom Line...........................................................................................................14 Chapter 3. Antibugging..........................................................................................................15 3.1 Beginning......................................................................................................................15 3.2 Writing Code.................................................................................................................16 3.3 Observation...................................................................................................................19 3.4 Documentation.............................................................................................................20 3.5 Developing....................................................................................................................22 3.6 Accident Prevention.....................................................................................................23 3.7 Tips for Reducing Complexity....................................................................................26 Chapter 4. Perl Pitfalls...........................................................................................................29 4.1 Syntactical Sugaring....................................................................................................29 4.2 The Hall of the Precedence........................................................................................34 4.3 Regular Expressions...................................................................................................37 4.4 Miscellaneous...............................................................................................................37 Chapter 5. Tracing Code.......................................................................................................44 5.1 Dumping Your Data.....................................................................................................45 5.2 Making it Optional........................................................................................................46 5.3 Raise the Flag..............................................................................................................47 5.4 At Your Command.......................................................................................................48 5.5 Taking the Long Way Around....................................................................................50 Chapter 6. Testing Perl Programs........................................................................................52 6.1 Inspection Testing........................................................................................................53 6.2 Unit Testing...................................................................................................................54 6.3 System or Regression Testing...................................................................................56 6.4 Saturation Testing........................................................................................................59 6.5 Acceptance Testing.....................................................................................................59 6.6 References....................................................................................................................60 iii Chapter 7. The Perl Debugger..............................................................................................61 7.1 Basic Operation............................................................................................................61 7.2 Starting..........................................................................................................................62 7.3 Getting Graphical.........................................................................................................71 Chapter 8. Syntax Errors.......................................................................................................74 8.1 Typo Pathologies.........................................................................................................75 8.2 A Menagerie of Typos.................................................................................................80 Chapter 9. Run-time Exceptions...........................................................................................83 9.1 Symbolic References..................................................................................................84 9.2 Check That Return Code!...........................................................................................88 9.3 Taking Exception to Yourself.....................................................................................89 9.4 Playing Catch-Up.........................................................................................................90 9.5 Confession Is Good for the Soul...............................................................................92 Chapter 10. Semantical Errors.............................................................................................93 10.1 A Bit Illogical...............................................................................................................93 10.2 Reading Directories...................................................................................................94 10.3 But What Did It Mean?..............................................................................................95 10.4 printf Formats Don't Impose Context.................................................................98 10.5 Conditional my............................................................................................................98 10.6 Bringing Some Closure.............................................................................................99 Chapter 11. Resource Failure.............................................................................................100 11.1 Optimize for People First, Resources Later.........................................................100 11.2 Benchmark It!...........................................................................................................101 11.3 Making Things Better..............................................................................................107 Chapter 12. Perl as a Second Language..........................................................................113 12.1 Tips for Everyman....................................................................................................113 12.2 Tips for the C Programmer.....................................................................................114 12.3 Tips for the FORTRAN Programmer....................................................................115 12.4 Tips for the Shell Programmer...............................................................................116 12.5 Tips for the C++ or Java Programmer..................................................................117 Chapter 13. Debugging CGI Programs.............................................................................120 13.1 CGI.............................................................................................................................120 13.2 Web Servers.............................................................................................................121 13.3 500—Server Error....................................................................................................121 13.4 Basics........................................................................................................................122 13.5 Security.....................................................................................................................123 13.6 Heading Off Errors...................................................................................................125 13.7 cgi-test.......................................................................................................................126 13.8 Eavesdropping.........................................................................................................126 13.9 CGI.pm......................................................................................................................129 13.10 Command Line Testing........................................................................................130 13.11 Dying Young...........................................................................................................130 13.12 Debugger Interaction............................................................................................131 13.13 ptkdb........................................................................................................................133 Chapter 14. Conclusion.......................................................................................................134 14.1 Finis...........................................................................................................................134 14.2 The End.....................................................................................................................134 14.3 This Really Is the End.............................................................................................135 Appendix A. Perl Debugger Commands...........................................................................136 A.1 General Syntax..........................................................................................................136 A.2 Commands.................................................................................................................137 A.3 Options........................................................................................................................141 A.4 Environment Variables..............................................................................................143 Appendix B. Perls of Wisdom.............................................................................................145

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值