Python, Java, Life, etc

          A blog of technology and life.

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            30 Posts :: 0 Stories :: 9 Comments :: 0 Trackbacks

          Example: The Carp Module

          This useful little module lets you do a better job of analyzing runtime errors-like when your script can't open a file or when an unexpected input value is found. It defines the carp(), croak(), and confess() fuNCtions. These are similar to warn() and die(). However, instead of reported in the exact script line where the error occurred, the fuNCtions in this module will display the line number that called the fuNCtion that generated the error. Confused? So was I, until I did some experimenting. The results of that experimenting can be found in Listing 15.6.

          Load the Carp module.
          Invoke the strict pragma.
          Start the Foo namespace.
          Define the
          foo() fuNCtion.
          Call the
          carp() fuNCtion.
          Call the
          croak() fuNCtion.
          Switch to the main namespace.
          Call the
          foo() fuNCtion.

          Listing 15.6  15LST06.PL-Using the carp() and croak() from the Carp Module
          use Carp;
          use strict;

          package Foo;
          sub foo {
          main::carp("carp called at line " . __LINE__ .
          ",\n but foo() was called");

          main::croak("croak called at line " . __LINE__ .
          ",\n but foo() was called");
          }

          package main;
          foo::foo();


          This program displays:

          carp called at line 9, 

          but foo() was called at e.pl line 18

          croak called at line 10,

          but foo() was called at e.pl line 18

          This example uses a compiler symbol, __LINE__, to iNCorporate the current line number in the string passed to both carp() and croak(). This technique enables you to see both the line number where carp() and croak() were called and the line number where foo() was called.

          The Carp module also defines a confess() fuNCtion which is similar to croak() except that a fuNCtion call history will also be displayed. Listing 15.7 shows how this fuNCtion can be used. The fuNCtion declarations were placed after the foo() fuNCtion call so that the program flow reads from top to bottom with no jumping around.

          Load the Carp module.
          Invoke the strict pragma.
          Call
          foo().
          Define
          foo().
          Call
          bar().
          Define
          bar().
          Call
          baz().
          Define
          baz().
          Call
          Confess().

          Listing 15.7  15LST07.PL-Using confess() from the Carp Module
          use Carp;
          use strict;

          foo();

          sub foo {
          bar();
          }

          sub bar {
          baz();
          }

          sub baz {
          confess("I give up!");
          }

          This program displays:

          I give up! at e.pl line 16

          main::baz called at e.pl line 12

          main::bar called at e.pl line 8

          main::foo called at e.pl line 5

          This daisy-chain of fuNCtion calls was done to show you how the fuNCtion call history looks when displayed. The fuNCtion call history is also called a stack trace. As each fuNCtion is called, the address from which it is called gets placed on a stack. When the confess() fuNCtion is called, the stack is unwound or read. This lets Perl print the fuNCtion call history.

          posted on 2005-02-18 03:49 pyguru 閱讀(561) 評論(0)  編輯  收藏 所屬分類: Perl
          主站蜘蛛池模板: 常山县| 滦南县| 拜城县| 通化县| 洛宁县| 乐山市| 韶关市| 盘锦市| 堆龙德庆县| 定结县| 大港区| 日照市| 台北市| 扎赉特旗| 远安县| 宁津县| 广州市| 道真| 温泉县| 高阳县| 新巴尔虎右旗| 政和县| 浦东新区| 三门县| 厦门市| 曲周县| 防城港市| 田阳县| 龙泉市| 安新县| 忻城县| 玉环县| 临沧市| 开封县| 合阳县| 新建县| 威远县| 南华县| 芮城县| 北海市| 施秉县|