This isn't really obfuscation, it's even vaguely useful, but see if you can figure out what Pipes.pm must be doing, given that this prints o-l-l-e-H
use strict; use warnings; use Pipes; print "hello" | fn { ucfirst($_[0]) } | fn { [ split('', $_[0]) ] } | fn { [ reverse(@{$_[0]}) ] } | fn { join('-', @{$_[0]}) } | fn { $_[0] . "\n" };
package Pipes; use strict; use warnings; use overload '|' => 'pipe'; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(fn); sub new { my ($class, $sub) = @_; bless { sub => $sub }, $class; } sub fn (&) { my ($sub) = @_; new Pipes($sub); } sub pipe { my ($self, $val) = @_; $self->{sub}->($val); } 1;
The exported 'fn' sub is prototyped to accept a sub as argument, and wraps that in a new Pipes object. The overloaded '|' operator applies the wrapped sub to its argument. '|' is left associative, and the left argument is evaluated first, so the arguments to the method 'pipe' are reversed.
perl -e 'print sub { "Hello @{[shift->()]}!\n" }->(sub{"World"})'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Functional Composition
by ikegami (Patriarch) on Dec 17, 2009 at 21:10 UTC | |
by billh (Pilgrim) on Dec 17, 2009 at 21:19 UTC | |
by ikegami (Patriarch) on Dec 17, 2009 at 21:22 UTC | |
by billh (Pilgrim) on Dec 17, 2009 at 22:02 UTC | |
by ikegami (Patriarch) on Dec 18, 2009 at 01:09 UTC | |
|
Re: Functional Composition
by BrowserUk (Patriarch) on Dec 17, 2009 at 21:04 UTC | |
by billh (Pilgrim) on Dec 17, 2009 at 21:38 UTC |