perlクイズ - Scalar::Lazyの実装

| 0 Comments | 0 TrackBacks | このエントリーをはてなブックマークに追加 このエントリーのはてなブックマーク件数

See also: http://blog.livedoor.jp/dankogai/archives/51059559.html


以下は、Scalar::Lazy 0.01 の実装です(0.03は機能を加えたのでこれより少し長い)。

package Scalar::Lazy;
use warnings;
use strict;
our $VERSION = sprintf "%d.%02d", q$Revision: 0.1 $ =~ /(\d+)/g;
use base 'Exporter';
our @EXPORT = qw/ delay lazy /;

sub new($&) { bless $_[1], $_[0] }
sub lazy(&) { __PACKAGE__->new(@_) }
*delay = \&lazy;

sub force($){
    my $pkg = ref $_[0];
    bless $_[0], $pkg . '::FORCE';
    my $val = $_[0]->();
    bless $_[0], $pkg;
    $val;
}

use overload (
    fallback => 1,
    map { $_ => \&force } qw( bool "" 0+ ${} @{} %{} &{} *{} )
);

1;    # End of Scalar::Lazy

以下、問題です。

  • overload は一体何のためにあるのでしょう?
  • force()は一体何をやっているのでしょう?
  • なぜわざわざbless()しなおしているのでしょう?

Dan the Lazy Perl Monger

No TrackBacks

TrackBack URL: http://perl-mongers.org/MT/mt-tb.cgi/41

Leave a comment

About this Entry

This page contains a single entry by dankogai [livedoor.com] published on June 2, 2008 2:41 AM.

Perl Best Practiceの使い方 was the previous entry in this blog.

perlでmicroformats is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 4.21-en