13.07.2015 Views

Perl 语言编程 - Linux教程

Perl 语言编程 - Linux教程

Perl 语言编程 - Linux教程

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

sub get_method {my ($self, $methodname) = @_;my $methref = sub {# 下 面 的 @_ 和 上 面 的 那 个 不 一 样 !return $self->$methodname(@_);};return $methref;}my $dog = new Doggie::Name => "Lucky",Legs => 3,Tail => "clipped";our $wagger = get_method_ref($dog, 'wag');$wagger->("tail");# 调 用 $dog->wag('tail').现 在 , 你 不 仅 可 以 让 Lucky 摇 动 尾 巴 上 的 任 何 东 西 , 就 算 词 法 $dog 变 量 已 经 跑 出 了 范围 并 且 现 在 你 看 不 到 Lucky 了 , 那 么 全 局 的 $wagger 变 量 仍 然 会 让 它 摇 尾 巴 —— 不 管它 在 哪 里 。8.3.7.1 用 闭 合 做 函 数 模 板拿 闭 合 做 函 数 模 板 可 以 让 你 生 成 许 多 动 作 类 似 的 函 数 。 比 如 你 需 要 一 套 函 数 来 生 成 各 种 不 同颜 色 的 HTML 字 体 变 化 :print "Be", red("careful"), "with that ", green("light), "!!!";red 和 green 函 数 会 非 常 类 似 。 我 们 已 经 习 惯 给 我 们 的 函 数 名 字 , 但 是 闭 合 没 有 名 字 , 因为 它 们 只 是 带 倾 向 性 的 匿 名 子 过 程 。 为 了 绕 开 这 个 问 题 , 我 们 将 使 用 给 我 们 的 匿 名 子 过 程 命229

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!