Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • See, this is exactly why I love Perl, cause there is always a something on CPAN that will make your life easier.

    use Sub::Current;

    print sub {
        my $num = shift;
        return $num < 2 ? 1 : $num * ROUTINE->($num - 1);
    }->(10);

    - Stevan

    • That's great, but solve my 'yield' problem and I'll really love you :)

      • I took the dog for a walk and thought about this a bit and realized where my scoping issue was. Here is a sugared version, probably could be a little cleaner still, maybe even with some subroutine attributes (sub foo : continuation { ... } or something).

        #!/usr/bin/perl

        use strict;
        use warnings;
        use Scalar::Util 'blessed';

        {
            package Foo;
            sub new { bless { val => $_[1] } => $_[0] }
            sub inc { $_[0]->{val} += $_[1] }
            sub val { shift->{val} }
        }

        our $current_continuation = sub {};
        sub yield { $current_continuation->(@_) }
        sub proc (&) {
            my $block = shift;
            my $cont  = $current_continuation;
            bless sub {
                local $current_continuation = $cont;
                $block->(@_)
            } => 'proc';
        }
        sub func {
            my ($name, $body) = @_;
            no strict 'refs';
            *{$name} = sub {
                local $current_continuation = pop @_
                    if blessed $_[-1] && $_[-1]->isa('proc');
                $body->(@_);
            };
        }

        func testy => sub {
            my ($args) = @_;
            my $foo = Foo->new(1);
            _testy($args, $foo, proc { yield($foo) });
        };

        func _testy => sub {
            my ($args, $somefoo) = @_;
            foreach my $item (@$args) {
                $somefoo->inc($item);
                yield();
            }
        };

        testy([1,2,3], proc { print $_[0]->val, $/ });

        1;

        - Stevan

        • Damn. I am doing the bit with passing around the sub, but it really does read like what you've done is much cleaner and actually makes it read like Ruby.

          • Actually took a look into rubyisms, I think that is just another case of Cozens "egoware" (only one version ever released and not meant to actually be used, only for people to look at and go "wow"). All the DB:: fiddling means it is bound to be very fragile with things like eval, etc.

            RE: reading like Ruby

            I actually find the desugared perl one to be the most readable since it doesn't obscure what is actually being done.

            - Stevan

        • Ruby's use of the 'yield' keyword caught me off guard. I suppose I'm used to how python uses it, as part of generators (rather than as simply a sub invocation). Well... here's how you might do something similar with Coro::Generator anyway :)

          use strict;
          use Coro::Generator;

          {
            package Foo;
            sub new { bless { val => $_[1] } => $_[0] }
            sub inc { $_[0]->{val} += $_[1] }
            sub val { shift->{val} }
          }

          my $inner_testy = generator {
            my $foo = pop;
            foreach my $item (@_) {