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
• See?(Score:1)

That doesn’t actually work because `\$fact` doesn’t exist at the time you call it.

So you simply use the Y combinator [perl.org]. :-)

• Re:(Score:2)

I had thought of that when I was writing this, but I confess that I never really understood why people were so hot and bothered about the Y combinator. There's a point when complexity overrides functionality. That being said, it may simply be a case of my being dense, but an awful lot of smart people seem to struggle with this :)

• Re:(Score:1)

Complexity of what? The Y combinator is easy to use: you return the recursive closure from another closure; the first argument of the outer closure can be used in the inner closure to return. That’s it.

The difficulty is in deriving the exact form of the combinator (which, of course, is a bit of brain-melt, which is why it’s become a sort of sport), not in using it.

• Re:(Score:1)

Err, the first argument of the outer closure can be used in the inner closure to recurse.

• You saw rubyisms?(Score:2)

rubyisms.pm has an idiomatic translation of "yield" (As well as "self" and "super"). As for perldoc -f, ri should do everything you want.
• Re:(Score:2)

I'm working with the 'yield' function from that now. And thanks for the tip about 'ri'. Very useful!

• Re:(Score:2)

Actually, I can't seem to use your 'yield' as a direct translation. I'm missing something fundamental. I put together this in Ruby (very similar to my actual problem):

class Foo
def initialize(val)
@val = val
end
def inc(some_val)
@val = @val + some_val
end
end

def testy(args)
foo = Foo.new(1)
_testy(args, foo) {
yield

• Sub::Current for the Win!(Score:1)

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

• Re:(Score:2)

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

• Re:(Score:1)

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 { \$

• Re:(Score:1)

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

• Re:(Score:2)

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.

• Re:(Score:1)

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.

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

- Stevan

• yieldness(Score:1)

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 (@_) {