Toby Inkster > PerlX-Perform > PerlX::Perform

Download:
PerlX-Perform-0.006.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.006   Source  

NAME ^

PerlX::Perform - syntactic sugar for if (defined ...) { ... }

SYNOPSIS ^

 my $foo = function_that_might_return_undef();
 perform { say $_ } wherever $foo;
 
 my $bar = function_that_might_return_undef();
 wherever $bar, perform { say $_ };

DESCRIPTION ^

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:

 my $foo = function_that_might_return_undef();
 perform { say $_ } wherever $foo;

But there is one before perform here:

 my $bar = function_that_might_return_undef();
 wherever $bar, perform { say $_ };

Gory Details

The implementation is pure Perl. The closest it gets to trickery is that the two functions defined by this package use prototypes.

perform

perform is a function can be called in two ways:

wherever

wherever is a function can be called in three ways:

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

Tail Calls

Both perform and wherever make extensive use of goto in order to conceal their usage on the call stack.

whenever

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/;

BUGS ^

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=PerlX-Perform.

SEE ALSO ^

http://www.modernperlbooks.com/mt/2012/02/a-practical-use-for-macros-in-perl.html.

Scalar::Andand.

AUTHOR ^

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE ^

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.

DISCLAIMER OF WARRANTIES ^

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.

syntax highlighting: