Changeset 13038

Show
Ignore:
Timestamp:
06/02/08 02:09:59 (5 years ago)
Author:
dankogai
Message:

Scalar::Lazy 0.03

Location:
lang/perl/Scalar-Lazy/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Scalar-Lazy/trunk/Changes

    r13037 r13038  
    22# Revision history for Scalar-Lazy 
    33# 
    4 # $Id: Changes,v 0.2 2008/06/01 16:29:17 dankogai Exp $ 
     4# $Id: Changes,v 0.3 2008/06/01 17:09:08 dankogai Exp dankogai $ 
    55# 
    6 $Revision: 0.2 $ $Date: 2008/06/01 16:29:17 $ 
     6$Revision: 0.3 $ $Date: 2008/06/01 17:09:08 $ 
     7+ t/04-once.t 
     8! lib/Scalar/Lazy.pm 
     9  Added 'once' option. 
     10 
     110.02 2008/06/01 16:29:17 
    712! lib/Scalar/Lazy.pm 
    813  POD fixes. 
  • lang/perl/Scalar-Lazy/trunk/MANIFEST

    r13033 r13038  
    88t/02-combinator.t 
    99t/03-tarai.t 
     10t/04-once.t 
    1011t/pod-coverage.t 
    1112t/pod.t 
  • lang/perl/Scalar-Lazy/trunk/README

    r13037 r13038  
    33 
    44VERSION 
    5     $Id: README,v 0.2 2008/06/01 16:29:34 dankogai Exp dankogai $ 
     5    $Id: README,v 0.3 2008/06/01 17:09:08 dankogai Exp dankogai $ 
    66 
    77SYNOPSIS 
     
    5151    There are various CPAN modules that does what this does. But I found 
    5252    others too complicated. Hey, the whole code is only 25 lines long! 
    53     Nicely fits in a good-old terminal screen. 
     53    (Well, was until 0.03) Nicely fits in a good-old terminal screen. 
    5454 
    5555    The closest module is Scalar::Defer, a brainchild of Audrey Tang. But I 
     
    7272 
    7373      Scalar::Lazy->new(sub { value }); 
     74 
     75    You can optionally set the second parameter. If set, the value becomes 
     76    constant. The folloing example illustrates the difference. 
     77 
     78      my $x = 0; 
     79      my $once = lazy { ++$x } 'init'; # $once is always 1 
     80      is $once, 1, 'once'; 
     81      is $once, 1, 'once'; 
     82      my $succ = lazy { ++$x }; # $succ always increments $x 
     83      isnt $succ, 1, 'succ'; 
     84      is $succ, 3, 'succ'; 
    7485 
    7586  delay 
  • lang/perl/Scalar-Lazy/trunk/lib/Scalar/Lazy.pm

    r13037 r13038  
    22use warnings; 
    33use strict; 
    4 our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; 
     4our $VERSION = sprintf "%d.%02d", q$Revision: 0.3 $ =~ /(\d+)/g; 
    55use base 'Exporter'; 
    66our @EXPORT = qw/ delay lazy /; 
    77 
    8 sub new($&) { bless $_[1], $_[0] } 
    9 sub lazy(&) { __PACKAGE__->new(@_) } 
     8sub new($&;$) {  
     9    my ($pkg, $code, $init) = @_; 
     10    if ($init){ 
     11        my $val = $code->(); 
     12        $code = sub { $val }; 
     13    } 
     14    bless $code, $pkg; 
     15} 
     16 
     17sub lazy(&;$) { __PACKAGE__->new(@_) } 
    1018*delay = \&lazy; 
    1119 
     
    3139=head1 VERSION 
    3240 
    33 $Id: Lazy.pm,v 0.2 2008/06/01 16:29:17 dankogai Exp dankogai $ 
     41$Id: Lazy.pm,v 0.3 2008/06/01 17:09:08 dankogai Exp dankogai $ 
    3442 
    3543=head1 SYNOPSIS 
     
    8189There are various CPAN modules that does what this does.  But I found 
    8290others too complicated.  Hey, the whole code is only 25 lines long! 
    83 Nicely fits in a good-old terminal screen. 
     91(Well, was until 0.03) Nicely fits in a good-old terminal screen. 
    8492 
    8593The closest module is L<Scalar::Defer>, a brainchild of Audrey Tang. 
     
    105113 
    106114  Scalar::Lazy->new(sub { value }); 
     115 
     116You can optionally set the second parameter.  If set, the value 
     117becomes constant.  The folloing example illustrates the difference. 
     118 
     119  my $x = 0; 
     120  my $once = lazy { ++$x } 'init'; # $once is always 1 
     121  is $once, 1, 'once'; 
     122  is $once, 1, 'once'; 
     123  my $succ = lazy { ++$x }; # $succ always increments $x 
     124  isnt $succ, 1, 'succ'; 
     125  is $succ, 3, 'succ'; 
    107126 
    108127=head2 delay