Manpages

NAME

perlfaq7 - 综 合 的 问 题 (2003/07/24 02:17:21)

DESCRIPTION 描 述

本 节 讨 论 综 合 的 Perl 语 言 问 题 , 不 适 于 在 其 他 所 有 段 落 中 讨 论 的 问 题 我 能 拿 到 Perl的 BNF/yacc/RE吗 ? 没 有 BNF , 但 是 你 可 以 从 源 代 码 的 perly.y 文 件 的 yacc 语 法 中 自 行 归 纳 , 如 果 你 足 够 勇 敢 的 话 。 语 法 依 赖 于 非 常 智 能 的 词 法 分 析 , 因 此 也 要 准 备 好 阅 读 toke.c。 用 Chaim Frenkel的 话 : "Perl的 语 法 无 法 被 简 化 到 可 以 用 BNF 表 示 。 解 析 Perl的 工 作 是 分 散 于 yacc、 lexer、 烟 雾 和 镜 子 之 间 。 "

$@%*这 些 符 号 是 什 么 意 思 ? 我 怎 么 知 道 何 时 该 使 用 他 们 呢 ? 它 们 都 是 类 型 限 定 符 号 , 在 perldata 中 详 述 :

    $ 标 量 值 , 数 字 , 字 符 串 或 引 用
    @ 数 组
    % 散 列 , 关 联 数 组
    & 子 程 序 , 也 就 是 函 数 , 过 程 , 方 法
    * 代 表 这 个 符 号 的 所 有 类 型 。 在 版 本 4中 , 可 以 用 作 指 针 , 但 是 在 新 的  perl 中 可 以 只 用 引 用 就 可 以 了有 些 其 他 的 符 号 你 可 能 会 碰 到 但 却 不 是 指 定 形 态 用 的 有 :

    <> 这 是 用 来 从 一 个 文 件 句 柄 里 输 入 一 份 记 录
    \ 取 某 样 东 西 的 引 用注 意

< FILE > 不 是 用 来 指 定 文 件 类 型 , 亦 非 此 句 柄 的 名 字 。 它 只 是 将 <>这 个 运 算 符 作 用 在 FILE这 个 句 柄 上 。 在 标 量 上 下 文 (scalar context) 中 , 它 自 FILE 把 手 一 次 读 入 一 行 (嗯 , 该 说 一 笔 记 录 , 参 看 $/), 在 序 列 情 境 (list context)下 , 则 一 次 将 全 部 的 内 容 读 入 。 当 对 档 案 使 用 开 、 关 或 其 它 <>之 外 的 动 作 、 或 甚 至 只 是 提 到 把 手 时 , 切 记 不 要 使 用 <>。 下 面 的 用 法 是 正 确 的 : "eof(FH)", "seek(FH, 0, 2)" 以 及 "copying from STDIN to FILE ". 字 串 加 引 号 或 使 用 分 号 及 逗 号 是 否 绝 对 必 要 /还 是 完 全 没 必 要 ? 通 常 一 个 没 有 冠 上 形 态 符 号 的 字 (bareword)是 不 需 被 纳 入 引 号 里 的 , 但 在 大 多 数 的 情 况 下 或 许 该 这 么 做 (在 "use strict" 下 则 是 必 须 的 )。 但 由 一 个 简 单 的 字 (不 能 是 一 个 已 定 义 的 副 函 数 之 名 称 )所 构 成 的 索 引 值 , 和 "=>" 左 端 的 运 算 子 , 都 会 被 视 为 已 纳 入 引 号 了 : 这 些 和 这 些 一 样
------------ ---------------
$foo{line} $foo{"line"}
bar => stuff "bar" => stuff
一 个 区 块 末 端 的 分 号 可 有 可 无 , 一 个 序 列 的 最 后 一 个 逗 号 亦 同 。 良 好 的 写 作 风 格 (参 看 perlstyle)中 建 议 除 了 在 单 行 程 式 (one-liners)的 情 况 外 都 将 他 们 加 上 去 :

    if ($whoops) { exit 1 }
    @nums = (1, 2, 3);
    if ($whoops) {
        exit 1;}
    @lines = (
        "There Beren came from mountains cold",
        "And lost he wandered under leaves",
    );我 如 何 跳 过 一 些 传 回 值 ?一 种 方 法 是 将 传 回 值 当 作 序 列 来 对 待 , 然 后 用 索 引 来 指 名 其 中 的 某 个 位 置 :

        $dir = (getpwnam($user))[7];另 一 种 方 法 就 是 在 等 号 左 端 用

undef 作 元 素 :

($dev, $ino, undef, undef, $uid, $gid) = stat($file); 也 可 以 用 一 个 列 表 片 段 来 仅 选 择 你 需 要 的 元 素 :

($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5]; 我 如 何 暂 时 滤 掉 警 告 讯 息 ? 如 果 正 在 运 行 Perl 5.6.0 或 更 高 版 本 , "use warnings" 编 用 可 以 对 警 告 如 何 产 生 进 行 很 好 的 控 制 。 参 见 perllexwarn 中 的 细 节

    {
        no warnings;          # 暂 时 关 掉 警 告 讯 息
        $a = $b + $c;         # 我 知 道 这 些 变 数 可 能 未 定 义}如 果 运 行 旧 版 本 的

Perl, 变 量 $^W (在 perlvar 中 有 记 载 ) 控 制 了 这 个 块 的 运 行 时 警 告 :

    {
        local $^W = 0;        # 暂 时 关 掉 警 告 讯 息
        $a = $b + $c;         # 我 知 道 这 些 变 数 可 能 未 定 义}注 意 , 像 所 有 的 标 点 符 号 变 数 一 样 , 目 前 不 能 对

$^W 用 my(), 只 能 用 local()。 什 么 是 一 个 扩 充 ? 一 种 从 Perl呼 叫 编 译 好 的 C程 式 码 的 方 法 。 阅 读 perlxstut是 个 多 了 解 扩 充 (extensions)的 好 方 法 。 为 何 Perl运 算 子 的 优 先 顺 序 和 C的 不 一 样 ? 事 实 上 它 们 是 相 同 的 。 所 有 Perl自 C借 过 来 的 运 算 子 都 具 备 与 原 来 在 C 中 相 同 的 优 先 顺 序 。 问 题 出 在 那 些 C没 有 的 运 算 子 , 特 别 是 那 些 将 其 右 方 一 律 当 成 序 列 情 境 对 待 的 函 数 , 例 如 print, chmod, exec等 等 。 这 类 的 函 数 被 称 作 序 列 运 算 子 ("list operators"), 在 perlop的 优 先 顺 序 表 中 就 是 这 么 称 呼 。 一 个 常 犯 的 错 误 像 是 :

unlink $file ⎪⎪ die "snafu"; 这 会 被 解 译 器 看 成 是 :

unlink ($file ⎪⎪ die "snafu"); 要 避 免 此 问 题 , 须 加 上 括 号 或 是 用 超 低 优 先 的 "or" 运 算 子 :

    (unlink $file) ⎪⎪ die "snafu";
    unlink $file or die "snafu";这 些 “英 文 的 ”运 算 子

(and, or, xor,及 not)是 刻 意 设 计 成 较 一 般 序 列 运 算 子 低 的 优 先 顺 序 , 这 就 是 为 了 解 决 前 述 的 状 况 。 另 一 个 拥 有 出 人 意 料 的 优 先 顺 序 者 为 指 数 。 它 甚 至 高 于 负 号 , 这 使 得 "-2**2"变 成 负 四 而 非 正 四 。 他 同 时 也 会 “向 右 靠 ”(right-associate), 意 思 是 说 "2**3**2" 代 表 二 的 九 次 方 , 而 不 是 八 的 平 方 。

Although it has the same precedence as in C, Perl’s "?:" operator produces an lvalue. This assigns $x to either $a or $b, depending on the trueness of $maybe:

($maybe ? $a : $b) = $x; 我 如 何 声 明 /创 建 一 个 数 据 结 构 ? 一 般 来 说 , 我 们 不 ’’声 明 ’’ 一 个 结 构 。 用 一 个 (通 常 是 匿 名 的 ) 散 列 的 引 用 (hash reference)即 可 。 参 看 perlref 以 及 perldsc, 里 面 有 更 多 资 料 。 以 下 是 一 个 范 例 :

    $person = {};                   # new anonymous hash
    $person->{AGE}  = 24;           # set field AGE to 24
    $person->{NAME} = "Nat";        # set field NAME to "Nat"如 果 你 要 的 是 更 严 谨 的 写 法 , 看 看

perltoot 。 如 何 创 建 一 个 模 块 ? 一 个 模 组 就 是 一 个 放 在 同 名 档 案 里 的 包 裹 (package)。 例 如 , Hello::There模 组 会 放 在 Hello/There.pm。 perlmod 里 有 详 尽 说 明 。 Exporter 也 会 很 有 帮 助 。 如 果 你 正 在 写 一 个 C 或 是 混 合 了 C及 Perl 的 模 组 , 那 么 你 就 该 读 perlxstut 。

The "h2xs" program will create stubs for all the important stuff for you:

  % h2xs -XA -n My::Module

The "-X" switch tells "h2xs" that you are not using "XS" extension code. The "-A" switch tells "h2xs" that you are not using the AutoLoader, and the "-n" switch specifies the name of the module. See h2xs for more details. 如 何 创 建 一 个 类 ?

perltoot 里 面 有 对 于 类 和 对 象 的 介 绍 , perlobj 和 perlbot 也 有 。 如 何 知 道 一 个 变 量 是 否 是 污 染 的 ? 可 以 使 用 Scalar::Util 模 块 中 的 tainted() 函 数 (可 从 CPAN 获 取 , 也 包 含 在 Perl 5.8.0 中 )。 参 见 perlsec 中 的 "Laundering and Detecting Tainted Data" 。 什 么 是 闭 包 ? 关 于 闭 包 的 说 明 , 请 看 perlref 。 闭 包 (closure)是 个 精 确 但 又 很 难 解 释 的 计 算 机 科 学 名 词 。 在 Perl 里 面 , 闭 包 是 以 匿 名 函 数 的 形 式 来 实 现 , 具 有 持 续 参 照 位 于 该 函 数 范 围 之 外 的 文 字 式 变 数 值 的 能 力 。 这 些 外 部 的 文 字 变 数 会 神 奇 地 保 留 它 们 在 闭 包 函 数 最 初 定 义 时 的 值 (深 连 结 )。 如 果 一 个 程 式 语 言 容 许 函 数 递 回 另 一 个 函 数 的 话 (像 Perl 就 是 ), 闭 包 便 具 有 意 义 。 要 注 意 的 是 , 有 些 语 言 虽 提 供 匿 名 函 数 的 功 能 , 但 却 无 法 正 确 处 理 闭 包 ; Python 这 个 语 言 便 是 一 例 。 如 果 要 想 多 了 解 闭 包 的 话 , 建 议 你 去 找 本 功 能 性 程 式 设 计 的 教 科 书 来 看 。 Scheme这 个 语 言 不 仅 支 援 闭 包 , 更 鼓 励 多 加 使 用 。 以 下 是 个 典 型 的 产 生 函 数 的 函 数 :

    sub add_function_generator {
      return sub { shift + shift };}


    $add_sub = add_function_generator();
    $sum = $add_sub->(4,5);                # $sum is 9 now.闭 包 用 起 来 就 像 是 个  函 数 样 板 , 其 中 保 留 了 一 些 可 以 在 稍 后 再 填 入 的 空 格 。

add_function_generator() 所 递 回 的 匿 名 函 数 在 技 术 上 来 讲 并 不 能 算 是 一 个 闭 包 , 因 为 它 没 有 用 到 任 何 位 在 这 个 函 数 范 围 之 外 的 文 字 变 数 。 把 上 面 这 个 例 子 和 下 面 这 个 make_adder()函 数 对 照 一 下 , 下 面 这 个 函 数 所 递 回 的 匿 名 函 数 中 使 用 了 一 个 外 部 的 文 字 变 数 。 这 种 指 名 外 部 函 数 的 作 法 需 要 由 Perl递 回 一 个 适 当 的 闭 包 , 因 此 那 个 文 字 变 数 在 匿 名 函 数 产 生 之 时 的 值 便 永 久 地 被 锁 进 闭 包 里 。

    sub make_adder {
        my $addpiece = shift;
        return sub { shift + $addpiece };}


    $f1 = make_adder(20);
    $f2 = make_adder(555);这 样 一 来

"&$f1($n)" 永 远 会 是 20加 上 你 传 进 去 的 值 $n , 而 "&$f2($n)" 将 永 远 会 是 555加 上 你 传 进 去 的 值 $n。 $addpiece 的 值 会 在 闭 包 中 保 留 下 来 。 闭 包 在 比 较 实 际 的 场 合 中 也 常 用 得 到 , 譬 如 当 你 想 把 一 些 程 式 码 传 入 一 个 函 数 时 :

    my $line;
    timeout( 30, sub { $line = <STDIN> } );如 果 要 执 行 的 程 式 码 当 初 是 以 字 串 的 形 式 传 入 的 话 , 即

’$line = <STDIN>’ , 那 么 timeout() 这 个 假 想 的 函 数 在 回 到 该 函 数 被 呼 叫 时 所 在 的 范 围 后 便 无 法 再 撷 取 $line 这 个 文 字 变 数 的 值 了 。 什 么 是 变 量 自 杀 , 我 应 该 怎 样 防 止 它 ? 变 数 自 杀 指 的 是 (暂 时 或 是 永 久 )地 失 去 一 个 变 数 的 值 。 造 成 这 个 现 象 的 原 因 是 做 范 围 界 定 的 my() 和 local()和 闭 包 或 foreach()回 圈 变 数 及 函 数 参 数 相 互 影 响 所 致 。 过 去 很 容 易 偶 尔 丢 失 变 量 , 现 在 就 困 难 多 了 , 可 以 试 试 这 段 代 码 :

    my $f = "foo";
    sub T {
      while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" }}
    T;
    print "Finally $f\n";有 叁 个

"bar" 加 进 去 的 $f 变 数 应 该 是 一 个 新 的 $f (因 为 "my $f" 在 每 个 循 环 都 应 该 创 造 一 个 新 的 区 域 变 数 )。 然 而 , 实 际 上 并 非 如 此 。 这 个 臭 虫 最 新 的 Perl 版 本 中 已 被 修 正 (在 5.004_05, 5.005_03 和 5.005_56 上 测 试 过 )。 如 何 传 递 /返 回 一 个 {函 数 Function, 文 件 句 柄 FileHandle, 数 组 Array,散 列 Hash, 方 法 Method, 正 则 表 达 式 Regex}? 除 了 正 规 表 现 式 这 个 特 例 外 , 你 需 要 以 传 参 考 值 的 方 式 传 资 料 给 这 些 物 件 。 参 看 perlsub 中 的 "Pass by Reference", 里 面 有 针 对 此 问 题 的 讨 论 , 以 及 perlref 里 面 有 引 用 的 资 讯 。 参 见 下 面 的 ’’Passing Regexes’’, 学 习 如 何 传 递 正 则 表 达 式 。 传 递 变 量 和 函 数 一 般 的 变 数 和 函 数 是 相 当 简 单 的 : 只 要 传 一 个 指 向 现 存 的 匿 名 变 数 或 函 数 的 参 考 值 即 可 :

    func( \$some_scalar );
    func( \@some_array  );
    func( [ 1 .. 10 ]   );
    func( \%some_hash   );
    func( { this => 10, that => 20 }   );

    func( \&some_func   );
    func( sub { $_[0] ** $_[1] }   );传 递 文 件 句 柄在

Perl5.6 中 , 你 可 以 用 标 量 变 量 表 示 文 件 句 柄 , 并 将 它 与 其 他 标 量 同 样 处 理

        open my $fh, $filename or die "Cannot open $filename! $!";
        func( $fh );
        sub func {
                my $passed_fh = shift;
                my $line = <$fh>;}在

Perl5.6 之 前 , 必 须 用 *FH"\*FH" 语 法 。 这 叫 做 "typeglobs"--参 见 perldata 中 的 "Typeglobs and Filehandles" 和 perlsub 中 的 "Pass by Reference"。 传 递 正 则 表 达 式 要 传 递 正 则 表 达 式 , 你 需 要 使 用 足 够 新 的 Perl 发 行 , 足 以 支 持 "qr//" 构 造 方 式 的 版 本 , 传 递 字 符 串 , 使 用 一 个 捕 获 异 常 的 eval, 或 者 其 他 更 聪 明 的 办 法 。 这 里 有 一 个 如 何 传 递 正 则 表 达 式 字 符 串 的 例 子 , 使 用 "qr//":

    sub compare($$) {
        my ($val1, $regex) = @_;
        my $retval = $val1 =~ /$regex/;
        return $retval;}
    $match = compare("old McDonald", qr/d.*D/i);注 意

"qr//" 如 何 允 许 在 后 面 加 上 标 志 。 这 个 模 式 在 编 译 期 被 编 译 , 尽 管 它 后 来 才 执 行 。 "qr//" 表 示 法 虽 然 好 用 , 但 是 直 到 5.005 发 行 中 才 引 入 。 在 那 之 前 , 你 必 须 用 不 直 观 的 办 法 。 例 如 , 如 果 没 有 "qr//" 的 话 :

    sub compare($$) {
        my ($val1, $regex) = @_;
        my $retval = eval { $val1 =~ /$regex/ };
        die if $@;
        return $retval;}

    $match = compare("old McDonald", q/($?i)d.*D/);确 保 你 没 有 任 何 这 样 的 东 西 :

    return eval "\$val =~ /$regex/";   # WRONG否 则 别 人 会 靠 双 引 号 括 起 来 的 字 串 以 及

eval 双 重 解 译 的 本 质 而 偷 偷 插 入 shell指 令 来 作 坏 事 。 例 如 :

$pattern_of_evil = ’danger ${ system("rm -rf * &") } danger’;

eval "\$string =~ /$pattern_of_evil/"; 想 学 非 常 非 常 聪 明 的 方 法 的 读 者 可 以 参 考 O’Reilly 出 的 Mastering Regular Expressions这 本 书 , 作 者 是 Jeffrey Friedl。 其 中 第 273页 的 Build_MatchMany_Function()特 别 的 有 趣 。 在 perlfaq2中 可 以 找 到 有 关 本 书 的 资 料 。 传 递 方 法 要 传 递 一 个 对 象 方 法 给 一 个 函 数 , 可 以 这 样 做 :

    call_a_lot(10, $some_obj, "methname")
    sub call_a_lot {
        my ($count, $widget, $trick) = @_;
        for (my $i = 0; $i < $count; $i++) {
            $widget->$trick();}}或 者 , 使 用 一 个 闭 包 来 包 含 这 个 对 象 , 它 的 方 法 调 用 及 参 数 :

    my $whatnot =  sub { $some_obj->obfuscate(@args) };
    func($whatnot);
    sub func {
        my $code = shift;
        &$code();}也 可 以 研 究

UNIVERSAL 类 别 中 的 can()方 法 (附 于 标 准 Perl 版 本 中 )。

How do I create a static variable? 就 像 与 Perl相 关 的 其 他 事 情 一 样 , ’’条 条 大 路 通 罗 马 ’’ (TMTOWTDI)。 对 其 他 语 言 来 说 叫 做 ’’静 态 变 数 ’’ (static variable)的 东 西 , 在 Perl里 面 可 能 是 一 个 函 数 私 有 的 变 数 (只 有 该 函 数 自 己 看 得 到 , 且 在 不 同 的 呼 叫 间 保 持 定 值 ), 或 是 一 个 档 案 私 有 (file-private)变 数 (只 有 同 一 个 档 案 中 的 函 数 才 看 得 到 )。 以 下 就 是 实 作 函 数 私 有 变 数 的 程 式 :

    BEGIN {
        my $counter = 42;
        sub prev_counter { return --$counter }
        sub next_counter { return $counter++ }}

prev_counter() 和 next_counter() 将 会 共 用 一 个 于 编 译 时 初 始 化 的 私 有 变 数 $counter。 要 声 明 一 个 档 案 私 有 (file-private)变 数 , 你 仍 然 得 使 用 my(), 将 它 放 在 档 案 开 头 处 最 外 围 。 假 设 现 在 是 在 Pax.pm 这 个 档 案 里 :

    package Pax;
    my $started = scalar(localtime(time()));
    sub begun { return $started }当 用

"use Pax""require Pax" 载 入 此 模 组 时 , 这 个 变 数 就 会 被 初 始 化 。 不 过 它 不 会 被 资 源 回 收 , 像 其 他 出 了 有 效 范 围 的 变 数 那 样 , 因 为 begun()函 数 要 用 到 它 , 但 是 没 有 其 他 函 数 能 撷 取 它 。 这 个 变 数 不 能 以 $Pax::started 的 形 式 来 撷 取 , 因 为 它 所 存 在 的 范 围 与 此 包 裹 无 关 。 它 存 在 的 范 围 是 这 个 档 案 。 可 想 见 地 , 一 个 档 案 里 可 以 放 好 几 个 包 裹 , 而 所 有 的 包 裹 都 撷 取 同 一 个 私 有 变 数 , 但 从 另 一 个 档 案 中 , 即 使 是 属 于 同 一 个 包 裹 (package), 也 不 能 取 得 它 的 值 。 参 见 perlsub 中 的 "Persistent Private Variables" 的 细 节 .

What’s the difference between dynamic and lexical (static) scoping? Between local() and my()?

local($x) 将 全 域 变 数 $x的 原 值 存 起 来 , 并 在 此 函 数 执 行 期 间 赋 予 一 个 新 值 , 此 值 可 以 从 此 函 数 所 呼 叫 的 其 他 函 数 里 看 见 。 这 整 个 步 骤 是 在 执 行 期 间 完 成 的 , 所 以 才 叫 做 动 态 范 围 选 取 (dynamic scoping)。 local()影 响 的 是 全 域 变 数 , 或 者 称 作 包 裹 变 数 或 动 态 变 数 。

"my($x)" 会 创 造 一 个 只 能 在 目 前 这 个 函 数 里 看 得 见 的 新 变 数 。 这 个 步 骤 是 在 编 译 期 完 成 (compile-time), 所 以 称 作 文 字 式 或 是 静 态 范 围 选 取 。 my()总 是 作 用 在 私 有 变 数 , 也 称 作 文 字 式 变 数 或 (不 当 地 )称 作 静 态 (范 围 选 取 )变 数 。 例 如 :

    sub visible {
        print "var has value $var\n";}

    sub dynamic {
        local $var = ’local’;   # 为 全 局 变 量 暂 时 赋 值
        visible();              # 调 用  $var 变 量}

    sub lexical {
        my $var = ’private’;    # 新 的 私 有 变 量  $var
        visible();              # (在  sub 作 用 域 之 外 不 可 见 )}

    $var = ’global’;

    visible();                  # prints global
    dynamic();                  # prints local
    lexical();                  # prints global你 可 以 发 现 在 整 个 过 程 中

’’private’’这 个 值 都 印 不 出 来 。 那 是 因 为 $var的 值 只 存 在 于 lexical() 函 数 的 区 块 里 面 , 对 它 所 呼 叫 的 函 数 来 说 是 看 不 到 的 。 总 结 来 说 , local()不 会 产 生 你 想 像 中 的 私 有 、 区 域 变 数 。 它 只 是 将 一 个 暂 时 的 值 授 予 一 个 全 域 变 数 。 如 果 你 要 的 是 私 有 的 变 数 , 那 么 my() 才 是 你 要 找 的 。 参 见 perlsub 中 的 "Private Variables via my()" 以 及 "Temporary Values via local()" 来 获 取 详 情 在 存 在 同 名 内 部 变 量 的 作 用 域 中 , 如 何 存 取 一 个 动 态 变 量 ? 如 果 你 知 道 你 所 在 的 是 哪 一 个 包 裹 (package)的 话 , 你 可 以 直 接 指 名 , 就 像 写 $Some_Pack::var 这 样 。 注 意 $::var 这 个 写 法 并 非 表 示 目 前 此 包 裹 (package) 内 的 动 态 变 数 $var, 而 是 指 在 main包 裹 (package) 里 的 那 个 , 就 等 价 于 $main::var

        use vars ’$var’;
        local $var = "global";
        my    $var = "lexical";

        print "lexical is $var\n";
        print "global  is $main::var\n";可 选 的 , 可 以 使 用 编 译 器 指 令

our() 来 在 当 前 静 态 作 用 域 中 引 入 动 态 变 量

        require 5.006; # our() did not exist before 5.6
        use vars ’$var’;
        local $var = "global";
        my $var    = "lexical";
        print "lexical is $var\n";

        {
          our $var;
          print "global  is $var\n";}深 连 接 和 浅 连 接 有 什 么 不 同 ?在 深 连 结 中 , 匿 名 函 数 中 所 用 到 的 文 字 式 变 数 值 是 以 该 函 数 产 生 时 所 在 的 范 围 为准 。 在 浅 连 结 中 , 这 些 变 数 值 是 以 函 数 被 呼 叫 时 所 在 的 范 围 为 准 , 如 果 在 这 个 范围 中 恰 巧 有 同 名 的 变 数 , 便 使 用 这 些 当 地 变 数 的 值 。

Perl总 是 使 用 文 字 式 变 数 (就 是 以 my()创 造 的 )式 的 深 连 结 。 然 而 , 动 态 变 数 (也 称 作 全 域 (global), 区 域 (local), 或 包 裹 (package)变 数 )在 功 效 上 是 浅 连 结 。 就 把 这 当 作 是 少 用 它 们 的 另 一 个 理 由 好 了 。 请 参 考 "什 么 是 闭 包 " 一 节 。 为 什 么

local()会 把 =号 右 边 以 序 列 情 境 来 对 待 。 而 <FH> 这 个 阅 读 的 动 作 , 就 像 Perl里 许 多 的 函 数 以 及 运 算 子 一 样 , 会 自 动 分 辨 出 自 己 被 呼 叫 时 所 在 的 情 境 并 且 采 取 适 当 的 作 法 。 一 般 来 说 , scalar()函 数 可 以 帮 点 忙 。 这 个 函 数 实 际 上 对 资 料 本 身 不 会 有 任 何 作 用 (与 一 般 所 认 为 的 相 反 ), 但 是 会 告 诉 它 所 作 用 的 函 数 要 以 对 待 纯 量 值 的 方 法 来 运 算 。 如 果 那 个 函 数 没 有 预 先 定 义 好 碰 到 纯 量 情 境 的 行 为 , 那 么 它 当 然 也 帮 不 了 你 (例 如 sort() 函 数 )。 然 而 , 在 以 上 这 个 例 子 (local...)中 , 只 要 省 略 括 号 便 可 强 制 使 用 标 量 情 境 :

    local($foo) = <FILE>;           # WRONG
    local($foo) = scalar(<FILE>);   # ok
    local $foo  = <FILE>;           # right其 实 在 这 个 例 子 中 , 或 许 你 该 改 用 文 字 式 变 数

(lexical variables), 不 过 会 碰 到 的 问 题 跟 上 面 一 样 :

    my($foo) = <FILE>;  # WRONG
    my $foo  = <FILE>;  # right如 何 重 定 义 一 个 内 建 函 数 , 操 作 符  或 者 方 法 ?为 什 么 要 这 么 做 ?  :

-) 如 果 你 要 覆 盖 掉 某 个 内 建 函 数 , 例 如 说 open(), 那 你 得 将 其 定 义 从 另 一 个 模 组 载 入 。 参 考 perlsub 中 的 Overriding Builtin Functions。 在 "Class::Template" 里 面 也 有 个 范 例 。 如 果 你 要 覆 盖 掉 一 个 Perl运 算 子 , 像 是 "+""**", 那 你 该 使 用 "use overload" 这 个 编 用 , 在 overload 中 有 记 载 。 如 果 你 要 覆 盖 父 类 别 (parent class)里 的 方 法 呼 叫 (method calls), 请 看 perltoot 中 的 Overridden Methods 。 调 用 函 数 时 &foo 和 foo() 的 形 式 有 什 么 不 同 ? 当 你 用 &foo的 方 式 呼 叫 一 个 函 数 时 , 你 等 于 让 这 个 函 数 撷 取 你 目 前 @_里 面 的 值 , 同 时 也 跳 过 原 型 定 义 (prototypes)不 用 。 这 表 式 此 函 数 抓 到 的 是 你 当 时 的 @_, 而 非 一 个 空 的 @_! 虽 然 严 格 讲 起 来 它 也 不 能 算 是 个 bug (但 是 在 perlsub里 面 是 这 么 说 的 )但 在 大 部 份 情 况 下 , 这 也 算 不 上 是 个 特 别 功 能 。 当 你 用 &foo()的 方 式 呼 叫 你 的 函 数 时 , 你 会 得 到 一 个 新 的 @_, 但 是 原 型 定 义 仍 然 会 被 避 开 不 用 。 在 一 般 情 况 下 , 你 该 用 foo()的 方 式 去 呼 叫 函 数 。 只 有 在 编 译 器 已 事 先 知 道 这 个 函 数 的 定 义 时 , 括 号 才 能 省 略 , 譬 如 当 这 个 函 数 所 在 的 模 组 或 包 裹 被 use (但 如 果 是 被 require则 不 行 )时 , 或 是 透 过 先 前 提 及 或 use subs宣 告 等 方 法 , 让 编 译 器 先 接 触 到 这 个 函 数 的 定 义 。 用 这 种 呼 叫 方 式 , 即 使 是 当 括 号 省 掉 时 , 你 都 会 得 到 一 个 干 净 的 @_, 不 会 有 任 何 不 该 出 现 的 旧 值 残 留 在 上 面 。 如 何 创 建 一 个 分 支 语 句 ? 这 个 问 题 在 perlsyn 文 件 里 有 更 详 尽 的 解 释 。 简 单 来 说 , 因 为 Perl本 身 已 提 供 了 多 种 不 同 的 条 件 测 试 方 法 可 供 使 用 (数 值 比 较 、 字 串 比 较 、 glob比 较 、 正 规 表 示 式 对 应 、 覆 盖 比 较 , 及 其 它 ), 所 以 并 没 有 正 式 的 case叙 述 语 法 。 虽 然 自 perl1起 这 就 一 直 是 许 多 人 期 盼 的 一 个 项 目 , 但 因 Larry无 法 决 定 怎 样 才 是 呈 现 这 功 能 的 最 好 方 法 , 因 此 还 是 将 它 略 掉 。 从 Perl 5.8 开 始 , 要 使 用 swtich 和 case, 可 以 使 用 Switch 扩 展 , 就 是 这 样 :

use Switch; 此 后 就 可 以 用 switch 和 case 了 . It is not as fast as it could be because it’s not really part of the language (it’s done using source filters) but it is available, and it’s very flexible.

But if one wants to use pure Perl, the general answer is to write a construct like this:

    for ($variable_to_test) {
        if    (/pat1/)  { }     # do something
        elsif (/pat2/)  { }     # do something else
        elsif (/pat3/)  { }     # do something else
        else            { }     # default}下 面 这 个 简 单 的

switch范 例 以 模 式 对 应 为 基 础 。 我 们 将 要 做 的 是 对 储 存 在 $whatchamacallit里 面 的 参 考 值 (reference)的 类 型 进 行 多 重 条 件 的 判 断 。 【 译 注 : $whatchamacallit 函 意 为 $what_you_might_call_it】

    SWITCH: for (ref $whatchamacallit) {
        /^$/            && die "not a reference";
        /SCALAR/        && do {
                                print_scalar($$ref);
                                last SWITCH;};

        /ARRAY/         && do {
                                print_array(@$ref);
                                last SWITCH;};

        /HASH/          && do {
                                print_hash(%$ref);
                                last SWITCH;};

        /CODE/          && do {
                                warn "can’t print function ref";
                                last SWITCH;};

        # DEFAULT
        warn "User defined type skipped";}

See "perlsyn/"Basic BLOCKs and Switch Statements"" for many other examples in this style.

Sometimes you should change the positions of the constant and the variable. For example, let’s say you wanted to test which of many answers you were given, but in a case-insensitive way that also allows abbreviations. You can use the following technique if the strings all start with different characters or if you want to arrange the matches so that one takes precedence over another, as "SEND" has precedence over "STOP" here:

    chomp($answer = <>);
    if    ("SEND"  =~ /^\Q$answer/i) { print "Action is send\n"  }
    elsif ("STOP"  =~ /^\Q$answer/i) { print "Action is stop\n"  }
    elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" }
    elsif ("LIST"  =~ /^\Q$answer/i) { print "Action is list\n"  }
    elsif ("EDIT"  =~ /^\Q$answer/i) { print "Action is edit\n"  }

A totally different approach is to create a hash of function references.

    my %commands = (
        "happy" => \&joy,
        "sad",  => \&sullen,
        "done"  => sub { die "See ya!" },
        "mad"   => \&angry,
    );

    print "How are you? ";
    chomp($string = <STDIN>);
    if ($commands{$string}) {
        $commands{$string}->();}
      else {
        print "No such command: $string\n";}如 何 捕 获 对 未 定 义 变 量 , 函 数 或 方 法 的 访 问 ?

perlsub 中 的 "Autoloading" 和 perltoot 中 的 " AUTOLOAD: Proxy Methods" 里 提 到 的 AUTOLOAD 方 法 让 你 能 捕 捉 对 于 未 定 义 函 数 与 方 法 的 呼 叫 。

When it comes to undefined variables that would trigger a warning under "use warnings", you can promote the warning to an error.

use warnings FATAL => qw(uninitialized); 为 什 么 找 不 到 包 含 在 同 一 个 文 件 中 的 方 法 ? 一 些 可 能 的 原 因 : 你 用 的 继 承 给 搞 混 了 、 你 拼 错 了 该 方 法 的 名 字 , 或 是 物 件 的 类 别 错 误 。 这 些 事 在 perltoot里 都 有 更 详 尽 的 说 明 。 同 时 你 也 可 以 用 "print ref($object)" 来 找 出 $object 这 个 物 件 是 被 归 到 哪 个 类 别 底 下 。 另 一 个 可 能 的 原 因 是 你 在 Perl还 不 知 道 这 个 包 裹 (package)存 在 之 前 便 将 某 个 类 别 名 称 在 间 接 式 物 件 语 法 中 使 用 (例 如 "find Guru "Samy"")。 最 好 是 在 开 始 使 用 你 的 包 裹 前 , 先 确 定 都 已 经 先 把 它 们 定 义 好 了 , 如 果 你 用 的 是 use 而 非 require的 话 , 这 件 事 便 会 自 动 处 理 好 。 不 然 的 话 , 确 定 你 使 用 箭 头 式 语 法 (例 如 , "Guru->find("Samy")"))。 在 perlobj 里 面 对 于 物 件 的 记 号 有 详 尽 解 释 。

Make sure to read about creating modules in perlmod and the perils of indirect objects in "Method Invocation" in perlobj. 如 何 找 到 当 前 的 包 ? 如 果 只 是 一 个 随 意 的 程 式 的 话 , 你 可 以 用 下 面 的 方 法 找 出 目 前 正 被 编 译 的 包 裹 为 何 :

my $packname = __PACKAGE__; 但 如 果 是 一 个 方 法 的 话 , 而 且 印 出 的 错 误 讯 息 中 要 包 含 呼 叫 此 方 法 的 物 件 (不 见 得 就 是 把 这 个 方 法 编 译 进 去 的 那 个 物 件 )则 :

    sub amethod {
        my $self  = shift;
        my $class = ref($self) ⎪⎪ $self;
        warn "called me from a $class object";}如 何 注 释 掉 大 块 的

perl 代 码 ? 用 内 嵌 POD格 式 的 方 法 把 程 式 码 变 注 解 。 将 要 注 释 掉 的 块 包 含 在 POD 标 记 内 , 例 如 "=for nobody""=cut" (标 志 着 POD 块 的 结 束 ).

    # 这 是 程 式
    =for nobody
    all of this stuff接 下 来 此 处 所 有 的 文 字 都 会 被 忽 略
    =cut
    # program continues

The pod directives cannot go just anywhere. You must put a pod directive where the parser is expecting a new statement, not just in the middle of an expression or some other arbitrary grammar production.

See perlpod for more details.

How do I clear a package?

Use this code, provided by Mark-Jason Dominus:

    sub scrub_package {
        no strict ’refs’;
        my $pack = shift;
        die "Shouldn’t delete main package"
            if $pack eq "" ⎪⎪ $pack eq "main";
        my $stash = *{$pack . ’::’}{HASH};
        my $name;
        foreach $name (keys %$stash) {
            my $fullname = $pack . ’::’ . $name;
            # Get rid of everything with that name.
            undef $$fullname;
            undef @$fullname;
            undef %$fullname;
            undef &$fullname;
            undef *$fullname;}}

Or, if you’re using a recent release of Perl, you can just use the Symbol::delete_package() function instead.

How can I use a variable as a variable name?

Beginners often think they want to have a variable contain the name of a variable.

    $fred    = 23;
    $varname = "fred";
    ++$$varname;         # $fred now 24

This works sometimes, but it is a very bad idea for two reasons.

The first reason is that this technique only works on global variables. That means that if $fred is a lexical variable created with my() in the above example, the code wouldn’t work at all: you’d accidentally access the global and skip right over the private lexical altogether. Global variables are bad because they can easily collide accidentally and in general make for non-scalable and confusing code.

Symbolic references are forbidden under the "use strict" pragma. They are not true references and consequently are not reference counted or garbage collected.

The other reason why using a variable to hold the name of another variable is a bad idea is that the question often stems from a lack of understanding of Perl data structures, particularly hashes. By using symbolic references, you are just using the package’s symbol-table hash (like %main::) instead of a user-defined hash. The solution is to use your own hash or a real reference instead.

    $USER_VARS{"fred"} = 23;
    $varname = "fred";
    $USER_VARS{$varname}++;  # not $$varname++

There we’re using the %USER_VARS hash instead of symbolic references. Sometimes this comes up in reading strings from the user with variable references and wanting to expand them to the values of your perl program’s variables. This is also a bad idea because it conflates the program-addressable namespace and the user-addressable one. Instead of reading a string and expanding it to the actual contents of your program’s own variables:

    $str = ’this has a $fred and $barney in it’;
    $str =~ s/(\$\w+)/$1/eeg;             # need double eval

it would be better to keep a hash around like %USER_VARS and have variable references actually refer to entries in that hash:

    $str =~ s/\$(\w+)/$USER_VARS{$1}/g;   # no /e here at all

That’s faster, cleaner, and safer than the previous approach. Of course, you don’t need to use a dollar sign. You could use your own scheme to make it less confusing, like bracketed percent symbols, etc.

    $str = ’this has a %fred% and %barney% in it’;
    $str =~ s/%(\w+)%/$USER_VARS{$1}/g;   # no /e here at all

Another reason that folks sometimes think they want a variable to contain the name of a variable is because they don’t know how to build proper data structures using hashes. For example, let’s say they wanted two hashes in their program: %fred and %barney, and that they wanted to use another scalar variable to refer to those by name.

    $name = "fred";
    $$name{WIFE} = "wilma";     # set %fred

    $name = "barney";
    $$name{WIFE} = "betty";     # set %barney

This is still a symbolic reference, and is still saddled with the problems enumerated above. It would be far better to write:

    $folks{"fred"}{WIFE}   = "wilma";
    $folks{"barney"}{WIFE} = "betty";

And just use a multilevel hash to start with.

The only times that you absolutely must use symbolic references are when you really must refer to the symbol table. This may be because it’s something that can’t take a real reference to, such as a format name. Doing so may also be important for method calls, since these always go through the symbol table for resolution.

In those cases, you would turn off "strict ’refs’" temporarily so you can play around with the symbol table. For example:

    @colors = qw(red blue green yellow orange purple violet);
    for my $name (@colors) {
        no strict ’refs’;  # renege for the block
        *$name = sub { "<FONT COLOR=’$name’>@_</FONT>" };}

All those functions (red(), blue(), green(), etc.) appear to be separate, but the real code in the closure actually was compiled only once.

So, sometimes you might want to use symbolic references to directly manipulate the symbol table. This doesn’t matter for formats, handles, and subroutines, because they are always global--you can’t use my() on them. For scalars, arrays, and hashes, though--and usually for subroutines-- you probably only want to use hard references.

What does "bad interpreter" mean?

The "bad interpreter" message comes from the shell, not perl. The actual message may vary depending on your platform, shell, and locale settings.

If you see "bad interpreter - no such file or directory", the first line in your perl script (the "shebang" line) does not contain the right path to perl (or any other program capable of running scripts). Sometimes this happens when you move the script from one machine to another and each machine has a different path to perl---/usr/bin/perl versus /usr/local/bin/perl for instance.

If you see "bad interpreter: Permission denied", you need to make your script executable.

In either case, you should still be able to run the scripts with perl explicitly:

        % perl script.pl

If you get a message like "perl: command not found", perl is not in your PATH , which might also mean that the location of perl is not where you expect it so you need to adjust your shebang line.

AUTHOR AND COPYRIGHT

Copyright (c) 1997-2002 Tom Christiansen and Nathan Torkington. All rights reserved.

This documentation is free; you can redistribute it and/or modify it under the same terms as Perl itself.

Irrespective of its distribution, all code examples in this file are hereby placed into the public domain. You are permitted and encouraged to use this code in your own programs for fun or for profit as you see fit. A simple comment in the code giving credit would be courteous but is not required.

译 者

陈 彦 铭 , 萧 百 龄 , 两 只 老 虎 工 作 室 跋 本 页 面 中 文 版 由 中 文 man 手 册 页 计 划 提 供 。 中 文 man 手 册 页 计 划 : https://github.com/man-pages-zh/manpages-zh