PerlX::Perform - syntactic sugar for if (defined ...) { ... }
my $foo = function_that_might_return_undef(); perform { say $_ } wherever $foo; my $bar = function_that_might_return_undef(); wherever $bar, perform { say $_ };
Executes some code if a given scalar is defined. Within the code block, the scalar is available as $_.
$_
Note that there is no comma before wherever here:
wherever
my $foo = function_that_might_return_undef(); perform { say $_ } wherever $foo;
But there is one before perform here:
perform
my $bar = function_that_might_return_undef(); wherever $bar, perform { say $_ };
The implementation is pure Perl. The closest it gets to trickery is that the two functions defined by this package use prototypes.
perform is a function can be called in two ways:
with a single coderef argument
In this case, perform returns a blessed version of that coderef; a so-called Manifesto object.
with a coderef argument followed by a scalar
Generates the Manifesto object, and executes the Manifesto on the scalar, returning the result.
Or rather, it has the effective result of doing the above. But it inlines the logic from PerlX::Perform::Manifesto.
wherever is a function can be called in three ways:
with a single scalar argument
In this case, wherever passes through the argument unchanged.
with a scalar argument and a Manifesto
In this case, wherever executes the Manifesto with the scalar argument.
with a scalar argument and a coderef
In this case, wherever turns the coderef into a Manifesto and executes it with the scalar argument.
This means that it's possible to do this:
my $manifesto = perform { say $_ }; wherever $foo, $manifesto; wherever $bar, $manifesto;
And indeed wherever does allow a little additional syntactic sugar by skipping over the string "perform" if it is used as the second parameter. Thus you can write:
my $manifesto = perform { say $_ }; wherever $foo, perform => $manifesto; wherever $bar, perform => $manifesto;
But because PerlX::Perform::Manifesto passes through any already-blessed coderefs, this will work too:
my $manifesto = perform { say $_ }; wherever $foo, &perform($manifesto); wherever $bar, &perform($manifesto);
Both perform and wherever make extensive use of goto in order to conceal their usage on the call stack.
goto
This is available as an alias for wherever, but is not exported by default. You need to request it like:
use PerlX::Perform qw/perform whenever/;
Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=PerlX-Perform.
http://www.modernperlbooks.com/mt/2012/02/a-practical-use-for-macros-in-perl.html.
Scalar::Andand.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2012 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
To install PerlX::Perform, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PerlX::Perform
CPAN shell
perl -MCPAN -e shell install PerlX::Perform
For more information on module installation, please visit the detailed CPAN module installation guide.