Skip to content

Instantly share code, notes, and snippets.

@peczenyj
Created May 19, 2016 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peczenyj/83cdb92472f28b71c1584219f1c805c3 to your computer and use it in GitHub Desktop.
Save peczenyj/83cdb92472f28b71c1584219f1c805c3 to your computer and use it in GitHub Desktop.
defer go-like in perl using Moo
package main;
use strict;
use warnings;
use feature 'say';
use Foo qw(defer);
{
say "before";
my $guard = defer { say "defer" };
say "after";
}
say "end";
package Foo;
use Exporter 'import';
use Moo;
use MooX::Types::MooseLike::Base qw(CodeRef);
has c => (
is => 'ro',
isa => CodeRef,
required => 1,
);
our @EXPORT_OK = qw(defer);
sub defer(&){
my ($callback) = @_;
return __PACKAGE__->new( c => $callback );
}
sub DEMOLISH {
my ($self) = @_;
$self->c->();
}
1;
$ perl a.pl
before
after
defer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment