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



Leave a comment