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 :)

      • Well, all the "yield" and "block" stuff is just sugar around your basic continuation passing. So here is the translation of what the ruby is actually doing.

        #!/usr/bin/perl

        use strict;
        use warnings;

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

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

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

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

        1;

        I tried wrapping some sugar around this, but it gets really messy with scoping. I suspect to do it right, you would need to do what Ruby does and make the subs into objects (Proc is what it is called in Ruby IIRC) and then wrap the sugar around that. Autobox might help here too, if for nothing more than removing the need to actually bless the subs.

        - Stevan