Perl6 also has a <i>macro</i> keyword for creating hygienic macros - <a href="http://en.wikipedia.org/wiki/Perl_6#Macros" rel="nofollow">http://en.wikipedia.org/wiki/Perl_6#Macros</a><p>For Perl5 you can use Devel::Declare (<a href="https://metacpan.org/module/Devel::Declare" rel="nofollow">https://metacpan.org/module/Devel::Declare</a>) or Devel::CallParser (<a href="https://metacpan.org/module/Devel::CallParser" rel="nofollow">https://metacpan.org/module/Devel::CallParser</a>) to achieve <i>macro</i> like effects.<p>For eg. Here's the Sweet.js <i>def add</i> macro using Devel::Declare...<p><pre><code> package MyDef;
use strict;
use warnings;
use base 'Devel::Declare::MethodInstaller::Simple';
sub import {
my $class = shift;
my $caller = caller;
my $arg = shift;
$class->install_methodhandler(
into => $caller,
name => 'def',
);
}
sub parse_proto {
my $ctx = shift;
my ($proto) = @_;
"my ($proto) = \@_;";
}
1;
</code></pre>
Then...<p><pre><code> use 5.016;
use warnings;
use MyDef;
def add ($x, $y) { $x + $y }
say add(1, 2); # => 3
</code></pre>
So when the compiler sees the <i>def</i> keyword then MyDef takes over and converts the line into...<p><pre><code> sub add { my ($x, $y) = @_; $x + $y }
</code></pre>
... then passes everything back to the perl parser to continue compiling the rest of the code.
Exactly why and how does this let me do stuff I couldn't do before with JS? The example used here looks like something that could be done just as easily with OOP at first glance...