Show
Ignore:
Timestamp:
03/24/09 04:49:29 (5 years ago)
Author:
rintaro
Message:

Checking in changes prior to tagging of version 0.02. Changelog diff is:

Index: Changes
===================================================================
--- Changes (リビジョン 22705)
+++ Changes (作業コピー)
@@ -1,4 +1,8 @@

Revision history for Perl extension Attribute::Generator


+0.02 Tue Mar 24 04:25:23 2009
+ - Release on CPAN
+ - Requires Coro 4.91
+

0.01 Tue Nov 4 04:16:32 2008

- - original version
+ - original version ( not released on CPAN )

Location:
lang/perl/Attribute-Generator/trunk
Files:
4 added
2 removed
6 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Attribute-Generator/trunk/Changes

    r22698 r31480  
    11Revision history for Perl extension Attribute::Generator 
    22 
     30.02    Tue Mar 24 04:25:23 2009 
     4        - Release on CPAN 
     5        - Requires Coro 4.91 
     6 
    370.01    Tue Nov  4 04:16:32 2008 
    4         - original version 
     8        - original version ( not released on CPAN ) 
  • lang/perl/Attribute-Generator/trunk/Makefile.PL

    r22797 r31480  
    33all_from 'lib/Attribute/Generator.pm'; 
    44 
    5 requires 'Coro'; 
     5requires 'Coro' => '4.91'; 
    66requires 'Attribute::Handlers'; 
    77 
  • lang/perl/Attribute-Generator/trunk/lib/Attribute/Generator.pm

    r22898 r31480  
    33use strict; 
    44use warnings; 
    5 our $VERSION = '0.01'; 
     5our $VERSION = '0.02'; 
    66 
    77use Attribute::Handlers; 
    8 use Coro::State; 
    9 #use Scalar::Util (); 
     8use Coro::State 4.91; 
    109 
    1110use base qw(Exporter); 
     
    1615    my($package, $symbol, $refent) = @_; 
    1716    no warnings 'redefine'; 
    18     *{$symbol} = sub { Generator::State->new($refent, @_); }; 
     17    *{$symbol} = sub { new Attribute::Generator::State $refent, @_ }; 
    1918} 
    2019 
    21 my @stack = (Coro::State->new()); # Generator stack; 
     20our @stack = (Coro::State->new()); # Generator stack; 
    2221 
    2322sub yield { 
    24     my $cur = pop @stack; 
    25     $cur->{_sent} = $_[0]; 
    26     $cur->transfer($stack[-1]); # back 
    27     delete $cur->{_sent}; # from send() 
     23    $stack[-1]{_sent} = \@_; 
     24    pop(@stack)->transfer($stack[-1]); 
     25    my $sent = delete $stack[-1]{_sent} or return; # from send() 
     26    wantarray ? @$sent : $sent->[0]; 
    2827} 
    2928 
    3029{ 
    31     package Generator::State; 
     30    package Attribute::Generator::State; 
    3231    use base qw(Coro::State); 
    3332 
     
    4241        }; 
    4342 
    44         $stack[-1]{_throw} = $@ if $@; 
     43        $stack[-2]->throw($@) if $@; 
    4544        while() { 
    46             my $cur = pop @stack; 
    47             $cur->transfer($stack[-1]); # back 
    48             delete $cur->{_sent}; 
     45            pop(@stack)->transfer($stack[-1]); 
     46            delete $stack[-1]{_sent}; # clear send()ed. 
    4947        } 
    5048    } 
     
    5856        push @stack, $self; 
    5957        $stack[-2]->transfer($self); # resume 
    60         return delete $self->{_sent} if exists $self->{_sent}; # from yield 
    61  
    62         # finished 
    63         die delete $self->{_throw} if exists $self->{_throw}; # dead. 
    64         return; 
     58        my $ret = delete $self->{_sent} or return; 
     59        wantarray ? @$ret : $ret->[0]; 
    6560    } 
    6661 
    67     sub send { $_[0]{_sent} = $_[1] }; 
     62    sub send { 
     63        shift->{_sent} = \@_; 
     64    } 
    6865 
    6966    sub __list__ { 
    7067        my($self) = @_; 
    7168        my @ret; 
    72         while(defined($_ = $self->next)) { 
    73             push @ret, $_; 
     69        while(my @tmp = $self->next) { 
     70            push @ret, @tmp; 
    7471        } 
    7572        \@ret; 
    7673    } 
    7774} 
    78  
    79 #sub iter { 
    80 #    my($obj) = @_; 
    81 # 
    82 #    if(ref $obj eq 'ARRAY') { 
    83 #        return Generator::State->new(sub { yield $_ for @{+shift} }, $obj); 
    84 #    } 
    85 #    if(ref $obj eq 'GLOB') { 
    86 #        return Generator::State->new(sub { my $o = shift; yield $_ while <$o> }, $obj); 
    87 #    } 
    88 #    if(Scalar::Util::blessed $obj) { 
    89 #        if($obj->can('next')) { # next method is a good thing :) 
    90 #            return $obj; 
    91 #        } 
    92 #        if(my $m = $obj->can('__iter__')) { 
    93 #            return iter($m->($obj)); # re-iter() to ensure returned object is a generator; 
    94 #        } 
    95 #        if(my $m = $obj->can('__list__')) { 
    96 #            return iter($m->($obj)); # re-iter() to convert listref to generator; 
    97 #        } 
    98 #    } 
    99 #    die "Could not convert to iterator"; 
    100 #} 
    101  
    102 #sub list { 
    103 #    my($obj) = @_; 
    104 #    if(ref $obj eq 'ARRAY') { 
    105 #        return $obj; 
    106 #    } 
    107 #    if(ref $obj eq 'GLOB') { 
    108 #        return [<$obj>]; 
    109 #    } 
    110 #    if(Scalar::Util::blessed $obj) { 
    111 #        if(my $m =(overload::Method($obj, '@{}') || $obj->can('__list__'))) { 
    112 #            return list($m->($obj)); # re-list() to ensure 
    113 #        } 
    114 #    } 
    115 #    die "Could not convert to list"; 
    116 #} 
    11775 
    118761; 
     
    13694  my $generator = fizzbuzz(1, 100); 
    13795   
     96  while(defined (my $val = $generator->next())) { 
     97    print "$val\n"; 
     98  } 
     99 
    138100  while(<$generator>) { 
    139101    print "$_\n"; 
     
    143105 
    144106Attribute::Generator realizes Python like generators using the power of L<Coro> 
    145 module. This module exports C<yield> function which is like C<yield> in Python. 
     107module. This module provides C<:Generator> CODE attribute which declares 
     108generator subroutines, and exports C<yield> function which is like C<yield> in 
     109Python. 
    146110 
    147111=head1 FUNCTIONS 
     
    152116 
    153117This CODE attribute declares generator. When generator subroutines are called, 
    154 returns an iterator object that has next() method. 
     118it returns an iterator object that has next() method. 
    155119 
    156120=item $generator->next() 
  • lang/perl/Attribute-Generator/trunk/t/01_basic.t

    r22797 r31480  
    2525} 
    2626 
     27sub gen_list :Generator { 
     28    yield 1,2,3; 
     29    yield 5,6; 
     30} 
    2731 
    2832{ # Basic 
     
    7579} 
    7680 
     81{ # yield list 
     82    my $gen = gen_list(); 
     83    is_deeply([$gen->next], [1,2,3]); 
     84    is_deeply([$gen->next], [5,6]); 
     85} 
     86 
    77871; 
  • lang/perl/Attribute-Generator/trunk/t/02_overload.t

    r22898 r31480  
    77 
    88sub itself:Generator { 
    9     my(@list) = @_; 
    10     yield $_ for @list; 
     9    yield $_ for @_; 
    1110} 
    1211 
     
    2524} 
    2625 
     26sub flatten:Generator { 
     27    yield @$_ for @_; 
     28} 
     29 
     30{ 
     31    my $gen = flatten([1,2],[11,12,13],[100]); 
     32    is_deeply([@$gen], [1,2,11,12,13,100]); 
     33} 
  • lang/perl/Attribute-Generator/trunk/t/03_exceptions.t

    r22706 r31480  
    1 use Test::More qw(no_plan); 
     1use Test::More tests => 2; 
    22 
    33use strict;